This documentation is archived and is not being maintained.

Walkthrough: Run Tests and View Code Coverage

To see what proportion of your project's code is actually being tested, use the code coverage feature of Visual Studio Team System testing tools. To do this, first edit the run configuration to indicate the assembly containing the code whose coverage you want to measure; then, run tests on that code. Detailed code coverage statistics appear in a window, and you can also see, line-by-line, which code has been tested.

Perform the steps in the procedure "Run a Unit Test and Fix Your Code" in Walkthrough: Creating and Running Unit Tests. This creates the two tests that you will run in the following procedure.

To run tests and view code coverage

  1. In Solution Explorer, note the name of your solution. If you used the project from Walkthrough: Creating and Running Unit Tests, the solution's name is Bank. This solution contains the code-under-test, or CUT.

  2. In Solution Explorer, under Solution Items, double-click the run configuration file, localtestrun.testrunconfig.

    The localtestrun.testrunconfig dialog box appears.

  3. Click Code Coverage.

  4. Under Select artifacts to instrument, check the box next to the Bank.dll whose path is indicated as <Solution Directory>\Bank\bin\Debug.

    NoteNote

    Selecting TestProject1.dll for instrumentation generates code-coverage information for the methods in your test project, not just for your CUT methods.

  5. Click Save and click OK.

  6. On the Test menu, point to Select Active Test Run Configuration. A submenu displays all the test run configurations in the solution. Put a check mark next to the run configuration you just edited, localtestrun.testrunconfig; this makes it the active test run configuration.

  7. In Test Manager, check the boxes beside CreditTest and DebitTest, and click the Run Tests button.

    The two tests run.

  8. On the Test Results toolbar, click Code Coverage Results.

    The Code Coverage Results window opens.

  9. In the Code Coverage Results window, the Hierarchy column displays one node that contains data for all the code coverage achieved in the latest test run. The test run node is named using the format <user name>@<computer name> <date> <time>. Expand this node.

  10. Expand the node for the assembly, Bank.dll, for the namespace, BankAccountNS, and for the BankAccount class.

  11. The rows within the BankAccount class represent its methods. The columns in this table display coverage statistics for individual methods, for classes, and for the entire namespace.

  12. Double-click the row for the Debit method.

    The Class1.cs source-code file opens to the Debit method. In this file you can see code highlighting. Lines highlighted green were exercised in the test run, and lines highlighted red were not. By scrolling, you can see the coverage for the other methods in this file.

    If you checked the box for TestProject1.dll in step 7, you can open Class1Test.cs, the source-code file containing your unit tests, to see which test methods were exercised. The same highlighting scheme applies: green indicates exercised code; red indicates a code path that was untraveled in the test run.

Show: