Hello Code Coverage! - Part 1
What can I say…I’m a big fan of hello world type walkthroughs. With new technology you often just want to bite off a little chunk to see what is has to offer. In that spirit, I’ll be taking a quick two part dive into running a simple code coverage scenario from the IDE. To do this, we will be integrating with the new test framework in Visual Studio Team System 2005. If you are interesting in running code coverage outside the IDE, check out joc’s blog posting on that topic.
For my example code, I’ll be using a simple function that returns either one or zero based on its input.
For the first step in running code coverage, we need to create a unit test. The fastest way to create a unit test is to simply right click on the function that you want to test. Then, from the context menu, select “Create Tests.”
On selecting “Create Tests” you will get the menu shown below. This menu lets you specify which functions to create tests for and the language to generate the tests in. For my example I’m only creating a test for the function TestFunction(), which is already selected. Before clicking ok, I changed the language type to C# as per my preference. When you click ok, you will be prompted to enter a name for the test project, which I chose to leave as the default. The test framework will then do the heavy lifting of generating the test for you.
The newly generated test project will be added to the Solution Explorer, as seen below. Opening the ProgramTest.cs file, we need to comment out the Assert.Inconclusive in the TestFunctionTest() function (Yeah, I should have picked a better name for my original function ;) ). You can see the line to comment out below. The Assert.Inconclusive line is automatically included in unit tests to remind you to verify your test results before you run the test.
Before we run the test, we need to choose what binaries to instrument for code coverage. In the solution explorer, click on the localtestrun.testrunconfig file. This will open up a new dialog window. In this dialog, select the Code Coverage tab from the right side. Select HelloCodeCoverage.exe in the “artifacts to instrument” section and then click the save button and close the dialog.
Now it is time to actually run our unit test. The first step is to open up the test view under the Test->Windows menu option. From the test view window (shown below) select the TestFunctionTest test and press the run button in the top left of the test view window.
On running the test, the test results window (shown below and top) will be displayed. We can see that the one test we ran completed successfully. Note that it does say “Test run error” above the successful test; this is just a warning message to inform us that CLR header flags needed to be updated on the executable being tested. But we are concerned with code coverage and not the actual test results, so just right click on the successful test run and select “Code Coverage Results.” This will change to the Code Coverage results from that test run as shown below.
Looking at the code coverage results above, we see that 75% of TestFunction was covered by our unit test. If we want to get our coverage numbers up, we’ll need to see what areas are not being covered by the test. To see actual code coverage, just double click on TestFunction in the code coverage results window. This brings up the source file containing TestFunction and highlights it to show the coverage of the test. The green areas were covered by the test, while the red areas were not covered. Since our input for the test was zero the code to return one was never exercised.
In the next walkthrough I’ll look at creating a second test to cover the uncovered code in TestFunction, and then joining the code coverage results from the two test runs.