If you’re looking for some free code coverage tools then the good news is look no further than OpenCover.
OpenCover can be run from the command line, which also means you can run it as part of your continuous deployment all for free, I like free.
If you’d like to get code coverage built into Visual Studio then look no further than the extension called OpenCover UI, this will give you code coverage results and show you which lines of code are covered by your tests, giving you no excuse to not test as much of your code where possible.
I created a very simple Calculator class library project and then add a class library for testing my calculator class, I’ll spare you showing the simple code, but here is a screen shot of me having ran the tests using OpenCover within Visual Studio.
The Above screen shows the lines in the class which are covered by tests in green, red are lines uncovered by any tests, below that is a break down showing the percentages of code covered and on the right is the test explorer showing the list of tests available.
If you want to run OpenCover within Visual Studio then add it via Extensions and Updates in the Tools menu of Visual Studio.
If you want to run OpenCover from the console then you can run it via the command prompt, doing it this way has much more flexibility as you can filter out test projects and generated code etc using the arguments you pass into the OpenCover executable.
Now once we have the code coverage within Visual Studio we can also get code coverage on our build server which is really what were after, due to OpenCover being an executable this is very easy to set up, you can find all the info you need to do this at the OpenCover Usage link.
In the second part of this blog post I’ll show how to generate a web page to show off these results which can be easily incorporated into your build server after each build, this will also show history of code coverage over time in case you want that feature too.