When doing TDD or at least unit testing, it is necessary to know how much of your code is covered by tests and, more importantly, which code isn’t. Since we migrated to NUnit the integrated Visual Studio 2010 code coverage does not do the job anymore for us (although I’ve found several articles on how to enable it, for some reason I couldn’t get it running).
There are two tools that you can use instead – one of them is NCover, which is a great tool but not free. The other one is PartCover, which is a free code coverage tool. I’ve opted to go with the latter. The main problem with PartCover is the almost non-existent documentation. After several hours of trying out different combinations of options I finally got it running and it does it job really good.
So first of all, download PartCover from here: https://github.com/sawilde/partcover.net4 and install it. Then, we must configure it for running our tests. Before we do this, we have to do one small step (which btw was the hardest one to find out) to make our nunit-console runner successfully run .NET 4.0 projects.
Open up Windows Explorer and go to your nunit installation directory (in my case it’s C:Program Files (x86)NUnit 2.5.10binnet-2.0). Open up nunit-console-x86.exe.config and add the following tag at the beginning, right after the <configuration> tag is opened:
<supportedRuntime version="v4.0.30319" />
This will make the nunit console run .NET 4.0 projects. You can do the same for all other nunit config files.
Once you’re done, open the PartCover browser and go to File->Run Target…
In the Executable file box, browse for the net-2.0nunit-console-x86.exe file. This should also set the working directory automatically.
In the working arguments enter the following text (replace the solution name and path with your own, but leave the double quotes): /noshadow "C:My ProjectsMyProjectMyProject.sln"
In the rules box, you need to add rules to include all assemblies that you want to check for code coverage and to exclude unnecessary assemblies. I use something like:
This will include all assemblies and namespaces that contiain “MyProject” in their name, but will exclude all assemblies and namespaces that contain unit tests.
Don’t forget to save the configuration for further use.
Then press start. It will run the console and show you a tree with the code and its coverage.