'How to generate line coverage report with vstest.console.exe?
When I run vstest.console.exe with the parameters, vstest.console.exe App.Tests.dll /collect:"Code Coverage" /Logger:html /EnableCodeCoverage App.dll, it only generates test execution report in the resulting html.
is it possible to generate line coverage report with this tool also or do I need more tools? As when I search for line coverage in C#, it takes me to https://docs.microsoft.com/en-us/visualstudio/test/using-code-coverage-to-determine-how-much-code-is-being-tested?view=vs-2019, and this one suggests vstest.console.exe, but it does not generate line coverage.
Solution 1:[1]
Overview
This article was of great help to me. Nevertheless, I am reproducing the steps here for posterity. I found my solution by using the following tools simulatenously.
- vstest.console.exe (VS.NET 2019 Community edition)
- OpenCover (via NUGET)
- ReporGenerator (via NUGET)
Background
- I have a simple .NET 48 Web API project
- I have Controller classes and I have written an unit test on these classes
Add reference to OpenCover NUGET package
I installed the package opencover , version 4.7.1221
The package comes along with the command line exe which we will be using for generating coverage information
C:\Users\username\.nuget\packages\opencover\4.7.1221\tools\OpenCover.console.exe
Add reference to ReportGenerator NUGET package
I installed the package ReportGenerator , version 5.1.6. The package comes along with the command line exe which we will be using for generating a readable HTML report on the code coverage information produced by the OpenCover.console.exe
tool
C:\Users\username\.nuget\packages\reportgenerator\5.1.6\tools\net47\ReportGenerator.exe
Running all the tools
Set the environment variables
set VSTEST_PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
set OPENCOVER_PATH=C:\Users\%USERNAME%\.nuget\packages\opencover\4.7.1221\tools\OpenCover.Console.exe
set REPORTGEN_PATH=C:\Users\%USERNAME%\.nuget\packages\reportgenerator\5.1.6\tools\net47\ReportGenerator.exe
set COVERAGE_RESULTS_FILE=C:\work\code-coverage\mycodecoverage.xml
set COVERAGE_REPORT_DIR=C:\work\code-coverage\myreport
Run the opencover tool to generate code coverage information
%OPENCOVER_PATH% -target:"%VSTEST_PATH%" -targetargs:C:\work\WebApp48\WebApplicationGenic\WebApplication1.Tests\bin\Debug\WebApplication1.Tests.dll -filter:"+[WebApplication1]*" -output:%COVERAGE_RESULTS_FILE%
%REPORTGEN_PATH% -reports:%COVERAGE_RESULTS_FILE% -targetdir:%COVERAGE_REPORT_DIR%
Generate a report using the ReportGenerator tool
%REPORTGEN_PATH% -reports:%COVERAGE_RESULTS_FILE% -targetdir:%COVERAGE_REPORT_DIR%
What are the files that got generated?
- The OpenCover will write the coverage information in a XML file(COVERAGE_RESULTS_FILE)
- The ReportGenerator tool will consume this XML file and produce a large number of HTML files in the specified output folder (COVERAGE_REPORT_DIR)
What to expect from the OpenCover tool?
A single large XML file - which I cannot make much sense of!
What to expect from the Report Generator tool?
HTML files
Index.html
We can locate our Controller classes.
Detailed coverage information
Click on ValuesController
line item in the index.html to see the detailed coverage information. We can see the lines of code that are not being tested.
Solution 2:[2]
Test Coverage is only supported in Enterprise license of Visual Studio. There are some 3-rd party tools you can use:
- dotCover - paid. Part of ReSharper suite
- openCover - open source.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sau001 |
Solution 2 | DanielS |