Calculating Coverage with NCover

28.10.2009 13:20Comments
NCover is an excellent tool that allows you to calculate how much of your code is being hit by your unit tests. Basically what it does is a profile of your application. So you can run your tests through any application and NCover will tell you the coverage of your tests. NCover is not an open source free tool, however it does have a Community Edition that will do the work for this tutorial which you can download from here after going through the registration. Since I have already showed how to run NUnit tests from the console, we are going to calculate the  coverage using NCover with NUnit. And taking advantage of the folder structure used we will continue with the following structure: ncover folder In order to make this available to the whole team, we are going to create a batch file (.bat) that will run the command for us (this will be usefull when integrating it with Nant). As I explained earlier, ncover has to execute an application in order to do the profiling. In our case, we want to run the NUnit tests, so this application will be nunit-console that rests in libnunit 2.4.8 folder. We covered the parameters that NUnit needs in order to run our tests in my previous post, so the only thing left to do is run ncover. The batch file will in the trunk folder, and will contain the following command: .NCoverNCover.console.exe  .nunitnunit-console.exe / .Concur.Lib.TestsbindebugConcur.Lib.Tests.dll We can optionally add a parameter in order to store the coverage output file into a different folder, like this: //x .build-resultcoverage.xml, so the batch file would look like this: .NCoverNCover.console.exe //x .build-resultcoverage.xml .nunitnunit-console.exe / .Concur.Lib.TestsbindebugConcur.Lib.Tests.dll And finally you can use your favorite xsl in order to visualize the results. There’s a very cool xsl file in the NCover folder called “coverage.xsl” which you can use by editing the xml to add the reference like this: <?xml version=”1.0″ encoding=”utf-8″ standalone=”no”?> <?xml-stylesheet type=”text/xsl” href=”coverage.xsl”?> Adding this file to the build-result folder is a good idea too. So we have successfully calculated our NCoverage coverage for our project, and left a batch file available to the whole team to run it in their local machine. Simple as it is, this leaves us one step closer to having a pretty decent build mechanism.

comments powered by Disqus