, , , , , , , ,

NCover logo

Today, we will cover a rather short topic concerning unit testing, and that id the code coverage. Code coverage is the evaluation of how much of your code has been tested, or more accurately speaking- how much of the code have the test methods visited. For example if we have the method:

 private static void Birthday(bool isBirthday) 
  if (isBirthday)
    Console.Writeline("It's your birthday!");

If we have a test that uses Birthday(true); we won’t test the lines in the else part, so we won’t have a 100% code coverage. To evaluate the code coverage you can use the build-in code coverage tool in Visual Studio, but it comes only with the Ultimate version. If you don’t have Visual Studio Ultimate, you can use other code coverage tools. One of these tools is NCover.

Using NCover

To use the NCover tool to test the code coverage of a VS Test Project, we need:

  • A Visual studio program and it’s test project
  • Installed NCover(same version as the Visual studio- x32 or x64)

We open the NCover Explorer and create a new Project(from the menu Project Actions => New Project). We should get the project settings window. In there we enter the project name and set the following fields:

  • Path to application to profile – in here we have to put the pat to the .exe file, which runs our Visual Studio test. That file is called MSTest.exe, and you can find it in the same directory in which your Visual Studio shortcut’s target is(for example, mine is in “C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe”)
  • Arguments for the application to profile – This is the test project we use to test our program, but not the .cs file, but the .dll file. In here we first put “/noisolation /testcontainer:” and right after “/testcontainer:” without space after it we put the location of our test project’s dll file
    (example: /noisolation /testcontainer:”D:\SAMPLE\SampleTestProject\Test.HelloWorld\bin\Debug\Test.HelloWorld.dll”)
  • Workign folder – Here we put the path to the bin folder of our Project, which we are testing (example: D:\SAMPLE\SampleTestProject\HelloWorld\bin)

Here is how my Code Coverage project’s settings ended like.Sample project settings
Mow we just click “close” and then click “Run Coverage”. We can now see which parts of the code have been covered by our test and how much of the project’s methods are covered(in percent).