Code Coverage for Unit Testing in Visual Studio

Wasana Panagoda
3 min readAug 29, 2022

--

Unit testing is a component of test-driven development (TDD), a pragmatic methodology that takes a meticulous approach to building a product by means of continual testing and revision. This testing method is also the first level of software testing, which is performed before other testing methods such as integration testing.

Unit tests are typically isolated to ensure a unit does not rely on any external code or functions. Unit testing is an important step in the development process, because if done correctly, it can help detect early flaws in code which may be more difficult to find in later testing stages.

Code coverage is a measurement of the amount of code that is run by unit tests — either lines, branches, or methods.

As an example, if you have a simple application with only two conditional branches of code (branch a, and branch b), a unit test that verifies conditional branch a will report branch code coverage of 50%.

Code Coverage Tools

There are 2 types of code coverage tools.

  • DataCollectors

DataCollectors monitor test execution and collect information about test runs. They report the collected information in various output formats, such as XML and JSON.

  • Report generators

Use data collected from test runs to generate reports, often as styled HTML.

Note : In this document, the focus is on data collector tools. To use Coverlet for code coverage, an existing unit test project must have the appropriate package dependencies, or alternatively rely on .NET global tooling and the corresponding coverlet.console NuGet package.

Get Code Coverage in a command prompt using Coverlet

  • Install CodeCoverlet using Command Prompt to the solution
  • Build the project
  • Navigate to the project .dll file in the Debug folder and execute following command in the command prompt. Then you will get the code coverage for the selected project.

Integrate with .NET test

The XUnit test project template already integrates with coverlet. Collector by default.

Note : The “XPlat Code Coverage “ argument is a friendly name that corresponds to the data collectors from Coverlet. This name is required but is case insensitive.

As part of the dotnet test run, a resulting coverage.TestLogin.xml file is output to the TestResults directory.

The XML file contains the results. This is a cross-platform option that relies on the .NET CLI, and it is great for build systems where MSBuild is not available.

Below is the example coverage.TestLogin.xml file.

Generate Reports

Reports can be generate using ReportGenerator.

  • To install the ReportGenerator NuGet package as a .NET global tool, use the dotnet tool install command which is given as below.
  • After it execute below command to generate the html reports in the Test Results folder.

Note : “-targetdir” defines where you want to save HTML files. In the above example all the HTML reports have been saved inside “coverageresults” folder.

  • After running the above command, an HTML file represents the generated report inside the “coverageresults” folder
CodeCoverage Report

--

--

Wasana Panagoda

M.Sc.in Information Systems, B.Sc.Special (Hones) in IT, ISTQB — CTFL