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:
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:
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!