I have looked around SO and the web for quite some time now and I am struggling to find any information for how to setup my C# project for unit tests.
I started on the NUnit main site http://www.nunit.org, the download includes a GUI to run your unit tests, sounds mint. Only when I try and point the GUI to my .exe, I get an error immediately saying that NUnit is not designed to be run with this framework. That got me thinking that maybe NUnit doesn't work with .net 4. Apparently it does, plenty of people online seem to state that they are running NUnit unit tests with .net 4 .... of course they don't seem to go on to say how.
Clicking around google I found what appears to be one of the core members of the NUnit development is a dude called Charlie Poole. I found he had a launchpad here: https://launchpad.net/nunit-xtp. The version numbers are newer than on the main NUnit site. When I downloaded the project however, it is just the codebase and solution for the NUnit framework with no GUI. There is a command line runner included but I really need something visual.
We do have jetbrains teamcity as a BMS so I am now going down the path of maybe trying to integrate NUnit into the build pipeline somehow. If anyone can shed any light on how they got NUnit working with an express edition of c# that would be wonderfully helpful.
Edit : I have the teamcity BMS running the tests now, so that part is no longer a problem. However, waiting for the build each time just to find a NullException error or something doesn't really cut it. I have to get the GUI runner working. I found this question NUnit isn't running Visual Studio 2010 code but even that isn't fixing it for me. The error has now updated to read version v4.0.30319, so the config changes have taken effect. Same old error though :(
Right I sussed it out. there is a very similar question here:
NUnit isn't running Visual Studio 2010 code
Only what it doesn't mention is that there are multiple config files for NUnit. I just assumed it would suffice to edit nunit.exe.config. This did indeed update the settings as I saw it in the error message. However, the nunit gui runner must make use of one of the other multiple .config files. I simply went through them all and added the two required settings.
Make sure that you run the correct .exe. One is called "nunit.exe" and it seems like the correct one to use. The other one is called "nunit-x86.exe" and if your app is 32bit then you have to run this one. If "nunit.exe" was names "nunit64.exe" I think I would have noticed that much sooner.
You must also make sure that you check the version number of the CLR matches the setting you are pasting into those config files. Use this link to work out your current CLR version number. How do I find out what CLR i am using? I am Microsoft Visual C# 2010 Express and my CLR version number is v4.0.30319
The unit tests should be in a seperate assembly (of type class library). If you point NUnit to that assembly, it will run. Same applies to the command line runner (which is normaly used in automated build processes).
The default NUnit distro includes a GUI runner called 'nunit.exe'. If using it, I set my debug target on my test assembly to load the GUI runner with the appropriate config.
Related
I have NUnit and MsTest in one project. Solution is common for whole team and only I have crash. Building solution works well, no errors. While running NUnit test I get exception: IO exception cannot load Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0.
I have one way reference, UnitTest project has reference to main Web project.
To write new tests I can use only MsTest but I would like NUnit.
I tried:
removing this dll -> but it crashes msTest (I want to have both
franeworks)
Upgrading dll but it makes another error (mayby I
could drill deeper this idea)
run as Administrator
In tests, Set default
process architecture x64 or x86
upgrading NUnit, NUnit Adapter ->
makes errors, solution is a bit old and I cannot actually upgrade so
much.
It seems Solution while testing only NUnit, tries to use MsTest dll, problem occures only for me in team. I've downloaded Repo recently compare to my team. Mayby I have someting newer than others and it's incompatible? I have VS2017. However others have VS2017 too.
I am stuck, please help
Solved after seting 'Copy Local' = true
I have a project, composed of a 3 C#/.NET Core class libraries, targeting .NET Standard 2.0. In addition, I have a XUnit test project, where I use FluentAssertions.
When working on the project, I use Visual Studio 2017 Enterprise, with Resharper Ultimate installed.
When running the full suite of tests, my SQL Server 2016 Express database is hit around 8100 times.
If I leave my profiler running while working in Visual Studio, I can see that the full suite of tests is executed, whenever I change code and do a build.
It's a bit of a nuisance, since it takes a good 1-1½ minute for the tests to finish, and often, I will only want to run a specific subset of the tests, when making incremental changes.
My problem: I can't seem to stop the tests running from running on build.
What puzzles me the most is, that I can't cancel the tests from Test explorer or Resharpers Test window, as they appear not to be running at all.
Also, the Test pane of the output window does not show anything except "....Discovering unittests" without progressing any further.
My impressions is, that from Visual Studios perspective, the tests are not running at all.
Getting the primary suspects out of the way:
The "Run tests after build" button in test explorer is not toggled
on.
Live unit testing appears to be disabled (The only option from the
menu is "Start")
I've spent hours browsing the net trying to figure out what is going on, with no luck. I'm hoping someone here can help me find out why the tests are running, and how to stop them.
This is my first ever StackOverflow question. I hope my questions meets the requirements for asking. If not, please let me know what to do better.
First I would advise you update VS. There was an issue in test discovery taking long time that was fixed in update 15.7 (See here)
If that doesn't do the trick I advise you try disabling this option in Tools > Options > Test:
As this might be attempting to analyse all assemblies in build output folder to discover tests.
I am trying to find some free way to run NUnit tests from VS2013. I tried the NUnit test adapter (both VSIX and NuGet package, but it doesn't seem to work (there the tests appear in the Test Explorer but are all grayed-out and can't be run). Is there something I need to do to get around this error/is there another extension out there that does this?
I am using NUnit 2.6.3.
Are you trying to run these tests for a Windows 8 application? It appears to be incompatible - or at least was when I last tried - I posted a similar thread as I was running into the same problem, and the posted solution did not work for me as I had a mix of W8 and older class libraries: Windows 8 App in Visual Studio 2012 - NUnit Test Adapter sees unit tests but does not run them
If I'm not mistaken, NUnit is the de-facto standard for unit testing, but I've just downloaded it, wrote a simple test, and then apparently I have to fire up the GUI and load my .exe assembly, which simply failed.
I tried editing
C:\Program Files (x86)\NUnit 2.5.7\bin\net-2.0\nunit.exe.config
As suggested in this question, but that didn't work either, so I tried downloading the nunit source code and compiling it in vs2010, but it doesn't even compile. Says punit.framework.dll could not be found. That solution says "does not contain a definition for AllTestsExecuted", so I'm getting a little frustrated here. You'd think there would be an easy-to-use-and-get-running framework for .net 4, no?
So my question is, how do I either get NUnit working, or is there another framework that will cause me less agony?
You don't have to use the NUnit GUI to run your tests. You can use TestDriven.NET from within Visual Studio. Also, if you happen to be using Resharper, that has a unit test runner which works with NUnit also.
If you're not doing anything out of the ordinary, I recommend Microsoft's Unit Testing Framework. I find it's VS integration too easy to even worry about NUnit. I agree NUnit seems to be the defacto standard, but if you're looking for something quick and easy. Microsoft's way is the easiest for a typical Visual Studio programmer IMHO.
I am not a C# programmer (fortunately ;-) ) but I've heard good things about xUnit. Tests can be run pretty much however you want (command line, GUI, Visual Studio integration, and more) and it looks reasonable simple to use.
For NUnit's GUI test runner, make sure you've selected the right framework version. Its in the "File" menu. If your test or any dependencies are 32-bit be sure you're running the 32bit version of the test runner.
Testdriven.net is a better test runner, but I like using NUnit's GUI runner too at times.
NUnit is infact very simple to use, so I would say that it's more likely that you are making a mistake somewhere, not the software.
Make sure you follow this guide.
Ensure that you have the [TestFixture] and [Test] attributes in the correct places and all the relevant assemblies referenced.
Make sure that you are loading the correct dll in the NUnit GUI.
If its the GUI that is the issue, you can use Resharper's unit testing feature in stead.
If you use Visual Studio 2010, you can use MSTest. Just click CTRL + ALT + R and it will run your tests and show the results in Visual Studio itself.
That same test-runner will also work for NUnit, if I am not mistaken.
I come from a Java/Eclipse background and I fear that I am spoiled by how easy it is to get JUnit and JMock running in Eclipse, and have that GUI with the bar and pass/fail information pop up. It just works with no hassle.
I see a lot of great options for testing in C# with Visual Studio. NUnit looks really nice because it contains unit and mock testing all in one. The trouble is, I can't figure out how to get the IDE display my results. The NUnit documentation seems to show that it doesn't automatically show results through the VS IDE. I found http://testdriven.net/, which seems to trumpet that is makes VS display these stats and work with multiple frameworks, but it isn't open source.
Is there anyway to get unit and mock testing working with the VS IDE like it does in Java with Eclipse?
On installing NUnit you get an NUnit.exe - use this to open and run your tests. It has an UI and shows pass/fails and shows output.
You can add a build action in Visual Studio that on a specific testing configuration will build, then immediately invoke NUnit on that dll.
EDIT: (more details)
In test project:
Project Properties -> Debug (set a build configuration - I use "NUnitDebug")
Start Action -> "Start external program": C:\Program Files\NUnit 2.5.3\bin\net-2.0\nunit.exe (use your own path)
Start Options -> Command line arguments: MyTestProject.dll (replace with the name of your DLL)
EDIT2: As brendan said, Moq is a good mock framework that can be used.
Resharper will let you do this and has a nice UI. I believe the core of it is NUnit. For the mock stuff you'll want to use Moq.
Resharper is not free/open source but is so worth the price.
If you are looking for something like Eclipse/JUnit, you shouldn't have tried Microsoft product line.
But the good news is that SharpDevelop has such nice integration with NUnit and it is open source. However, it aims as an alternative to VS, not an addon for VS.
You could read ASP.NET MVC Test Framework Integration Walkthrough and run your tests from the VS test runner.
Have you tried using the Testing projects in Visual studio? They're practically identical to nUnit, and can be run simply by hitting F5.
For mocking, chose whichever suits you, We're looking at Moq for Silverlight support.