Wednesday 1 April 2009

Code coverage using PartCover and NUnit

Question:
I looked for a free open-source tool to calculate code coverage for my NUnit tests.

Answer:
NCover has been a good choice until they went commercial. (very bad)

NCover is dead, long live PartCover.

PartCover is an open-source project for code coverage under GNU GPL. That also means, that you're not allowed to commercialize non-free derivatives.
PartCover does most of the things most of us would demand of such a tool. And don't forget - if you really want a new feature, you can add it yourself and help the community.


Sample setup.

I have a business logic assembly "VokAdapt.BLL" that I test using NUnit with a fixture which sits in the assembly "VokAdapt.BLL.Test".
I want PartCover to start NUnit, execute the tests and get the coverage of the business assembly.

1. Open PartCover GUI.
2. Fill in Target Settings.
2.a The "Executable File" here is NUnit.exe. You might want to choose the console version.
2.b The "Working Directory" is the directory that contains your test assembly.
2.c The "Working Arguments" is the test assembly simple name.
2.d Add a rule that tells PartCover which assemblies/classes to consider.



3. Start PartCover. (That will run the target application, here NUnit)
3.a Execute your tests.
4. Close NUnit and view results



You see very nice stats of your coverage and you can even inspect the code.
I get 36% coverage here. That isn't brilliant, so I better stop blogging and write some tests :)

If you like to integrate PartCover into VS2008 read Vlad's blog entry
PartCover has been added to SharpDevelop 3.