In this post I am talking about using Visual Studio code coverage tools to find code coverage with NUnit tests. In my current project we were using NUnit to write unit test cases. Now only we are started moving the NUnit tests to MS Tests for unit testing. It is very difficult and tedious job; also some changes required in the testing logic too. I faced the main issue with the code coverage; it is very easy to do with MS Test, because everything is integrated to Visual Studio. Just run the unit tests, right click on the results, select the code coverage results from the context menu, it will display the code coverage. But if you are using NUnit, you wont get this much flexibility, I don’t think that won’t be much issue compare to re-writing the NUnit test cases to MS tests for code coverage.
Hope you wrote the library and unit test cases.
- First we need to inject the code coverage instrumentation code to the assembly, to do this, go to the command line and type
“C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Performance Tools\ vsinstr.exe” [assembly] / COVERAGE
This will inject instrumentation code to the assembly. If the assembly is successfully instrumented, it will display a message like this
Successfully instrumented file [assembly]
- Next start the coverage monitor, to do this again type
“C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Performance Tools\ VSPerfMon.exe” /COVERAGE /OUTPUT:[assembly.coverage]
It will start coverage monitor (profile) server, and it will display a message like
Started in Stand Alone Mode
- Now using NUnit you can run the Unit Test cases.
- After executing all the test cases you can close the NUnit application.
- To view the coverage, you need to stop the coverage monitor(profiler), you can do this by executing the following command in another command line.
“C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools\VSPerfCmd.exe” /SHUTDOWN
This command will shutdown the profile monitor.
Now open the coverage file in Visual Studio to see the coverage. If you have pdbs, you can even see source highlighting as per the coverage.
An example screenshot of code coverage results source code.
Here is the color codes
- RED : uncovered
- GREEN : covered
- YELLOW : partly covered
Thanks to Pushkaraj
Note: You need to un-sign the assemblies first, before you start converage.