Freigeben über


Collecting Code Coverage Data when Running Web/Load Tests

This blog post will show you how to collect code coverage data from your asp.net applications while running a web test.  In this post I will go over how to collect coverage data when running your asp.net app against the development server and against IIS.

 

First let’s create a very simple web app to test.  Please follow these steps.

 

1) Create a new Web Site.  File - > New -> Web Site…

2) Then select ASP.Net web site

3) Add a label and a button to the default web page.

4) Double click the button to add an event handler for clicking the button.  Set the code of the page to the following:

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            Label1.Text = "foo";

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (Label1.Text.Equals("foo"))

        {

            Label1.Text = "bar";

        }

     else

        {

            Label1.Text = "foo";

        }

    }

}

 

 

5) Compile and run the web app to make sure it is working.  When clicking on the button the label should switch between foo and bar.

 

Now we should be ready to collect coverage data.  First we need to add a test project to the solution.  Please do the following:

 

1) Right click on the Solution in solution explorer

2) Select Add - > New Project

3) Select a Test Project

 

Development Web server

Next we will set up a web test which uses the development web server

1) Right Click on the default.aspx page in the web site project and select view in browser.  This will launch the development web server and bring up the default.aspx page in IE.

2) Right click on the test project and select Add -> Web Test …   This will create a web test and launch IE.

3) Go back to the IE which has the website running and copy the URL.

4) Now go back to the IE which was launched when creating a web test.  Paste the URL.

5) Click the button one time

6) Click the stop button on the recorder.

 

Now we have a web test.  Before we can collect code coverage, we need to configure the web test to launch a new instance of the development server when starting a web test.  Please do the following:

 

1) Click the Parameterize web server button. 

2) Click Change button

3) Select Use ASP.NET Development Server.  The default info should be filled out.  Check out this help link if it is not: https://msdn.microsoft.com/en-us/library/ms184806.aspx   It will describe what should be entered.  Here is what dialog should look like:

 

 

4) Click OK.

5) Click OK.

 

Now we are ready to run the test.  Click the Play button on the web test editor.  Verify the test plays back successfully.  Next we need to configure run to collect Code Coverage Data.

 

1) Double click the run configuration file in the solution items folder.

2) Click the Code Coverage Tab

3) Then click the check box for the web site

4) Click OK.

 

Run the test again.  This time when it completes we can open the coverage results.  To open the coverage results do the following:

1) Go to Test -> Windows -> Code Coverage Results.

2) Expand the tree and you will see something like the following:

 

 

From this test you can see that we covered 6 of the 8 blocks in the Button1_Click method.  If you double click that row in the window, it will open the source file and show you what code has been covered and what code has not been covered.

 

 

 

IIS

Collecting coverage data against an IIS server is a little trickier.  When collecting data against IIS, you need to do the following:

 

1) Use a command line tool to instrument the binaries/web app you want to collect data on

2) Start the coverage monitor

3) Run your Web/Load tests

4) Stop the coverage monitor

5) Import the .coverage file into VS to look at results.

 

So when running against IIS, there is no need to modify your run configuration files. VS will not be able to instrument the binaries for apps running on IIS.  The steps for how to perform the instrumentation and starting/stopping the coverage monitor is covered in detail in the following post: https://blogs.msdn.com/graycode/articles/AspNetOffRoadProfilingArticle.aspx

 

Note there are different things that you need to do to collect data for compiled dlls and dynamically compiled code such as aspx pages.

 

Once you have the environment setup to collect data, you simply run the web/load tests which you have recorded against your application.

Comments

  • Anonymous
    July 08, 2008
    This blog post will show you how you can collect code coverage data when running web and load tests: