PartCover - a code coverage tool
Update: you should now use OpenCover, which is a newer code-coverage tool by the author of PartCover.
Unfortunately it’s not documented well (misleadingly, even) so here’s a short tutorial.
You can download the installer from SourceForge project and install the usual way. This post is based on build 4.0.10705 (latest at this time).
After you install, you can just copy DLLs and EXEs to any location and run it from there. I keep the binaries in the source repository so that I can run code coverage tests from my test scripts without the need to have PartCover installed.
A note about 64-bit Windows
The 4.0 version can’t handle 64-bit mode. While .NET applications consist of platform-independent bytecode, when the bytecode is executed, it can be JIT-ed in 32-bit mode (the only option on 32-bit Windows and possibility in 64-bit Windows) or 64-bit mode. For that reason, both PartCover and the application being tested need to be forced into 32-bit mode.
For PartCover, run
[most recent Windows SDK]\bin\CorFlags.exe PartCover.exe /32BIT+ /Force
For your application, you need to compile it in 32-bit mode. When using
csc compiler, use
/platform:x86. In Visual Studio IDE, set Platform
Target in Build tab of project properties to x86 (default is
In my project, I have a special target for code coverage that compiles
The basic flow is:
- run your executable under PartCover.exe
- inspect the results (generated by PartCover.exe as an XML file)
Basic option for PartCover:
--target <taget>: name of the the .NET executable to test
--output <output-file>: name of the XML file with results
--include <[Assembly]class>: which assemblies/classes to include in code coverage. Can be used multiple times
--exclude <[Assembly]class>: which assemblies/classes to exclude from code coverage. By default [mscorlib]* and [System*]* are excluded. Can be used multiple times
An example invocation:
PartCover --target bin\UnitTests.exe --include [NachoDB]* --include [UnitTests]* --output partcover.xml
What it means:
- we run code coverage on
bin\UnitTests.exe.NET executable and all the assemblies it uses
- the executable uses NachoDB.dll assembly, so ask for code coverage
--include) of all classes in NachoDB and UnitTests assemblies. As you can see, you can use ‘*’ as a simple regular expression matcher with the expected meaning
- the results of code coverage are saved in
Looking at the results
PartCover includes PartCover.Browser application that can be used to configure and run code coverage tests and browse the results.
To browse existing results use
File/Open Report... menu and open
partcover.xml file. Also, use
Views/View Coverage Details menu -
results are not useful without it.
On the left you’ll see a list of assemblies with their classes and methods. When you click on a method, on the right you’ll see source code of that method annotated with code coverage information (lines with green background have been executed):
Alternatively, you can adapt for your purposes my python script that generates set of html files with the results of code coverage.
There are alternatives, but not great ones.
Expensive edition of Visual Studio has a code coverage built in. However, it’s a total disaster in usability and I was unable to get it to work.
What I expected is that it works like code profiling: I choose the binary, arguments and environment variables, run it, the results show up.
Visual Studio has other ideas. It seems that if I want to do code coverage from UI, I have to create a project specifically for that. It’s insane and I didn’t even try.
There is a way to do it from command line, by instrumenting the binaries with a cmd-line tool and running it with code coverage service process running (a byzantine and over-complicated setup, if you ask me). I tried this method following posts scattered on the internet but I ended up with empty code coverage results and no error message to tell me what’s wrong. Thank you, Microsoft, for wasting my time.
NCover used to be free and open-source tool but is now commercial. The few screenshots they have on their website look good but they won’t give you a trial version unless you give them your e-mail address and I wont.
|« Searching for available DBA name in San Francisco||•||Converting PartCover results to html »|