Code Coverage and Web Tests

Download sample code for this blog entry here.

Solution Tree

Code Coverage can be a helpful metric to see how much of your code is hit by the tests you have written. Although it isn’t a way to measure how good your code is, it will at least help you to see what parts of your code have no automated tests written against them.

However, you can run into some difficulties when trying to do code coverage for tests that hit a web site. Let’s look at an example. Suppose you are creating a web application, and you want to write tests against it. Some of the tests will be unit tests that test out specific classes in isolation. Other tests will be integration tests that use HTTP to test the website. You can see the sample solution structure at the right.

First, we’re going to test the SampleLogic class in the Logic project. Here is the SampleLogic class:

public class SampleLogic
public int val;
public SampleLogic(int value)
{ this.val = value; }
public int Double()
{ return val * 2; }