Hello Code Coverage! – Part 2

            In my last walkthrough on code coverage I showed how to get code coverage data for a simple function that I was testing. Below is the code view of the coverage results from the unit test on the function TestFunction. Remember that the green code is the code that was covered by our unit test; while the red code was not covered.

 

            Our unit test input “0” to the function TestFunction, so the code in the “if( input > 0 )” section was not covered. In today’s walkthrough, we’ll look at creating a second unit test to cover the code that our first test did not cover. Then we will join together the two code coverage results to see if we can get our coverage of the TestFunction function up to 100 percent.

 

            The first step is to create a new test for TestFunction. I’m going to do this by jumping to the ProgramTest.cs file in my test project, copying the TestFunctionTest function, pasting it below TestFunctionTest and then changing the name to TestFunctionTest2. Pictured below are the two tests in ProgramTest.cs. Also I changed the input from zero to one and the expected return value from zero to one.

 

            After creating the new test, it will show up in the test view along with the original TestFunctionTest. From the test view, select TestFunctionTest2 and run it. Looking back at the instructions in my last walkthrough, access the code coverage results for the run of TestFunctionTest2 (Pictured below).

 

 

            As you can see from the colored source code, this test covered the “if( input > 0 )” branch in our code. So if we were to combine the results of our two tests, we would hope to have 100 percent code coverage of the TestFunction function. In the code coverage results (pictured above) there is a little icon on the task bar that looks like two boxes joined by a green plus. Clicking this box will bring up the merge menu (pictured below) listing all the test runs for this session. I have two runs listed, my first run of TestFunctionTest and my second run of TestFunctionTest2.

 

            By selecting the two test runs and clicking ok, I’ve created a new set of results for the merged files. Pictured below are the merged results. As you can see from the code and from the data, we now have 100 percent code coverage of TestFunction using our two tests.

 

Comments

  • Anonymous
    May 25, 2005
    Visual Studio Team System

    Yesterday marked the one-year anniversary of the public announcement of...
  • Anonymous
    June 09, 2005
    I’ve pulled together all of the technical articles and walkthroughs from the various team member blogs...