Using PartCover and NUnit for Code-Coverage

If you’re looking around for decent code-coverage tools, you’ll hear a lot about NCover, which is now commercial, and quite expensive.

There’s a free, alternative tool called Part Cover.NET. It’s a bit old (hasn’t been updated since ~2008), but it does the job quite well.

You can download PartCover from the SourceForge page.

Once you’ve done that, setting it up can be a bit tricky for lack of information available. You need, as with any application, to specify where the testing-tool is (in our case, NUnit) and where the test-files binary is (so it can run the tests and calculate code-coverage).

Here’s a screenshot of some working configuration from clicking on File > Run Target:

PartCover settings and rules

The data in the fields are:

  • Executable File: Points to your testing exe file. In our case, we use the NUnit console exe file. This is what it runs against the tests.
  • Working Directory: The directory of your executable file.
  • Working Arguments: Arguements you pass into your executable file to get the tests to run. In the case of NUnit, it takes in a DLL file that contains all of your annotated test-cases and tests.
  • Rules: Specifies which classes (not methods, but only classes) that you want to generate coverage for. Let’s look at a sample positive and negative rule:
    • +[*]com.deengames.AutoDungeon.core*: Generate coverage for all of the classes in the com.deengames.AutoDungeon.core namespace, and all sub-namespaces (eg. com.deengames.AutoDungeon.core.controllers will be included, but core.deengames.AutoDungeon.tests will not). The [*] means, look in any project.
    • -[*]*Delegate: Don’t generate coverage for any class that ends in the word “Delegate.” This is because delegates have all inherited methods, and we don’t need to really test those.

Once you’re ready, hit Start to start. You will see a pop-up for the NUnit console exe file running the tests, then you should see results, like so:

AutoDungeon code-coverage report in PartCover

At this point, I recommend you go to Run Target again and save your configuration into an XML file that you check in to your source-control. You will add and remove classes and namespaces over time. You can click Cancel to return to the coverage report.

The left side shows you a hierarchical tree of namespaces and classes, and code coverage at each namespace, sub-namespace, and class-level. Once you go to Views > View Coverage Detail, you’ll see the panel on the right. Red lines of code are not covered by any test, and green lines are executed in at least one test.

You can also see method-level coverages. As a rule of thumb, 80% is pretty decent for code-coverage (since 100% is sometimes very hard and obscure to attain).

Also, as a reminder, code-coverage does not mean your tests are good. Code coverage means, some code, somewhere, in some test, hits that line; it could even just be setup code that doesn’t check anything! So use this as a guideline, not a hard-and-fast rule to tell you if you’re done testing.

And that’s NUnit and PartCover in a nutshell!

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Tools, Wndows Forms and tagged , , . Bookmark the permalink.

4 Responses to Using PartCover and NUnit for Code-Coverage

  1. Pingback: Tweets that mention Using PartCover and NUnit for Code-Coverage : C# City --

  2. Dave says:

    Just in case anyone else runs into the same problem as me:
    If the path to your dll has any spaces in it, you will need to enclose it in quotes in the ‘Working Arguments’ text box.

  3. Ashiq Alibhai, PMP says:

    @Dave thanks, I really appreciate you tracking me down on my website and on Stack Overflow to post this answer :)

  4. anon says:

    For 64bits change the Executable File to nunit-console-x86.exe

    If you see empty reports on 64bits system this might be the solution

    Partcover only works on assemblies executing in a 32 bits environment

    This solution only works to test assemblies that are compiled for x86 or any cpu

Leave a Reply

Your email address will not be published. Required fields are marked *