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://learn.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.
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.
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
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
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
%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%
%REPORTGEN_PATH% -reports:%COVERAGE_RESULTS_FILE% -targetdir:%COVERAGE_REPORT_DIR%
A single large XML file - which I cannot make much sense of!
We can locate our Controller classes.
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.