If you write unit tests and use NUnit test framework this may be helpful. I decided to write this simple step by step project configuration because I tend to set it up on every new project but keep forgetting all its details of how to do this. Setting it up is simple and a one-time only process for each test project.
You can always get NUnit test framework directly from the web on http://www.nunit.org/ and install it on your machine but it may be better to use a more solution-specific approach by using NuGet NUnit package that installs it only in a particular solution. Getting it using NuGet can either be done as outlined on the linked package page by using package manager console or using the package manager GUI by doing this:
- Right-click on project in Visual Studio Project Explorer; a popup context menu appears;
- Click on Manage NuGet Packages... menu option which will open package manager simple to use GUI;
- Wait for the dialog window to load packages from the web repo and then select NUnit;
This will automatically add NUnit library and tools to your solution folder and add relevant assembly references in your test project. Having a solution-specific NUnit version makes it future proof if newer NUnit versions are not backwards compatible. So one less thing to think about.
Executing unit tests
NUnit is a fine library, but it offers no Visual Studio integration per-se. It has an external program compiled either as a console app or a GUI. It runs your unit test assembly and executes any unit tests within.
Developers not having additional non-free Visual Studio tools usually run NUnit GUI tool and run those tests manually. But it's much easier if you can easily just hit F5 or Ctrl-F5 in Visual Studio that would compile unit tests and run the NUnit GUI tool automatically for you while also executing all unit tests on application start. The good thing is that you can also debug your unit tests this way in case you wanted to check something that seems to be invalid in your test code.
In order to automatically start NUnit test runner GUI tool you have to follow these steps to configure your test project to be capable of running/debugging unit tests:
- Open your test project properties by right-clicking on project in Visual Studio Project Explorer ans selecting option Properties...;
- Select Debug tab in project properties window;
- Set Start action as Start external program and point it to your local solution NUnit package installation; if you used NuGet package this is where you'll find your NUnit folder:
your solution folder\packages\nunit126.96.36.19992\tools\nunit.exeOf course with the right NUnit version in folder name.
- Set Command line arguments to point to your test project assembly file like:
- Set Working directory to point to your test project's debug folder like:
- Save project configuration properties and you're done;
I usually also configure my solution so that I set Startup project as Current selection. This makes it possible that hitting F5 runs my currently selected project. If I have a unit test file open it will run (start debugging) my unit test project, but if I'm currently working on ie. my web application file, it will automatically start debugging my web application.
Debugging test assemblies compiled for .Net 4.0
If you're trying to debug your test assembly that is compiled for :net version 4.0 or later you may have problems hitting your breakpoints. Visual Studio simply says that no debug symbols have been loaded for your assembly. The problem is that NUnit is running under .Net 2.0, but your test assembly is compiled for .Net 4.0.
To still keep the capability of automatically running your tests by hitting F5 you will have to edit
nunit.exe.config that's in the same folder as
nunit.exe test runner GUI. All you have to do is to add these three lines directly under
<configuration> configuration element:
2: <supportedRuntime version="4.0" />
This is all you need to know to configure your project to run NUnit unit tests directly from Visual Studio by starting NUnit test runner GUI. If you have any additional suggestions regarding this, please leave your comment below.