How to get code coverage report in donetcore 2 app

2020-05-08 08:54发布

问题:

I am new to dotnet core 2.0, not sure how to extract code coverage report with

dotnet test command.

Looking for help from the community.

回答1:

OpenCover is an open source code coverage tool for dotnet core. You can use it in conjunction with ReportGenerator to get coverage reports.

https://github.com/OpenCover/opencover https://github.com/danielpalme/ReportGenerator



回答2:

Here is my .bat file to generate dotnet core.

This will "pop" an index.htm file.

This is just a .bat file version of what I found here:

https://gunnarpeipman.com/aspnet-core-code-coverage/

Note, I have a few "3.1" references. It should not make a difference with 2.x. Just tweak the below to your version/world.

Thankfully, the dotnet-core version was MUCH easier than the dotnet-framework version (my answer for dotnet framework here : MSTest Code Coverage )

The below shows how to get results from 1 OR MORE UnitTests.csproj and have the html-report be a "merge" off all of them. (as described here : Is there anyway to merge cobertura coverage xml reports together? )

REM Make a .bat file like ZzzCoverageReports.bat and put the below in it.  Save the .bat file to the same directory as your .sln file





REM DEVELOPERS, going forward, if you have an alternate path, please put in a (new) IF-EXIST check instead of hard coding to a single specific path
IF EXIST "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0.16\dotnet.exe" set __dotNetExe=C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0.16\dotnet.exe
IF EXIST "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.13\dotnet.exe" set __dotNetExe=C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.13\dotnet.exe
IF EXIST "C:\Program Files (x86)\dotnet\dotnet.exe" set __dotNetExe=C:\Program Files (x86)\dotnet\dotnet.exe
IF EXIST "C:\Program Files\dotnet\dotnet.exe" set __dotNetExe=C:\Program Files\dotnet\dotnet.exe


set __fullDirectory=%~dp0%__subdirectory%


REM Start __datetimeStampString
for /f "tokens=2-4 delims=/ " %%g in ('date /t') do (
set mm=%%h
set dd=%%g
set yy=%%i
)
set __mydate=%yy%%mm%%dd%

for /f "tokens=1-2 delims=: " %%j in ('time /t') do (
set hh=%%j
set mn=%%k
)
set __mytime=%hh%%mn%
set __datetimeStampString=%__mydate%___%__mytime%
REM End __datetimeStampString


set __outputFilesDirectory=%__fullDirectory%ZzzTempOutputFiles\
set __testResults=%__outputFilesDirectory%TestResults_%__datetimeStampString%\
set __coverageResults=%__outputFilesDirectory%CoverageReports_%__datetimeStampString%
set __toolsPath=%__outputFilesDirectory%ToolsPath

RD %__testResults% /S /Q
RD %__coverageResults% /S /Q

RD %__outputFilesDirectory% /S /Q
MD %__outputFilesDirectory%
MD %__testResults%
MD %__coverageResults%


REM install reportgenerator locally
call "%__dotNetExe%" tool install dotnet-reportgenerator-globaltool --tool-path "%__toolsPath%"


set __slnShortName=My.Solution.sln
set __slnFullName=%__fullDirectory%%__slnShortName%


set __unitTestCsProjFullName001=%__fullDirectory%..\ProjectOne.UnitTests.csproj
set __unitTestCsProjFullName002=%__fullDirectory%..\ProjectTwo.UnitTests.csproj
set __unitTestCsProjFullName003=%__fullDirectory%..\ProjectThree.UnitTests.csproj

set __trxLogShortFileName001=My.ProjectOne.UnitTests.trx
REM sometimes if a project targets different frameworks, you get something like MyCoverletOutputXYZ.netcoreapp3.1.xml  just be aware.
set __coverageInputParameterShortFileName001=MyCoverletOutput001.xml


set __trxLogShortFileName002=My.ProjectTwo.UnitTests.trx
REM sometimes if a project targets different frameworks, you get something like MyCoverletOutputXYZ.netcoreapp3.1.xml  just be aware.
set __coverageInputParameterShortFileName002=MyCoverletOutput002.xml


set __trxLogShortFileName003=My.ProjectThree.UnitTests.trx
REM sometimes if a project targets different frameworks, you get something like MyCoverletOutputXYZ.netcoreapp3.1.xml  just be aware.
set __coverageInputParameterShortFileName003=MyCoverletOutput003.xml


REM now define a wild card for all the coverage cobertura xml files
set __coverageGeneratedShortNameWildCard=MyCoverletOutput*.xml


call "%__dotNetExe%" restore "%__slnFullName%" 

REM Below does not seem necessary.  which makes this process much faster.
REM call "%__dotNetExe%" build "%__slnFullName%" /p:Configuration=Debug /flp:v=diag;logfile="%__outputFilesDirectory%%__slnShortName%_Manual_DotNetExe_Build_31_DebugVersion_LOG.log" --framework netcoreapp3.1


REM Note, (PackageReference "coverlet.msbuild") AND (PackageReference "coverlet.collector")  must be in unit test project for COVERAGE to work.  The tell-tale is you get a trx file but none of the "MyCoverletOutput.xml" files.

REM find the relative path to your UNIT TEST csproj.  Also note the CoverletOutput may generate a slightly different filename if you have multiple targets for your build.  Aka, MyCoverletOutput.xml gets written as MyCoverletOutput.netcoreapp3.1.xml on my 3.1 targeted project
call "%__dotNetExe%" test "%__unitTestCsProjFullName001%" --logger:trx;LogFileName="%__testResults%%__trxLogShortFileName001%"  /p:CollectCoverage=true /p:CoverletOutput="%__testResults%%__coverageInputParameterShortFileName001%" /p:CoverletOutputFormat=cobertura
call "%__dotNetExe%" test "%__unitTestCsProjFullName002%" --logger:trx;LogFileName="%__testResults%%__trxLogShortFileName002%"  /p:CollectCoverage=true /p:CoverletOutput="%__testResults%%__coverageInputParameterShortFileName002%" /p:CoverletOutputFormat=cobertura
call "%__dotNetExe%" test "%__unitTestCsProjFullName003%" --logger:trx;LogFileName="%__testResults%%__trxLogShortFileName003%"  /p:CollectCoverage=true /p:CoverletOutput="%__testResults%%__coverageInputParameterShortFileName003%" /p:CoverletOutputFormat=cobertura

REM the string-length of the FULL file name of file
REM "(blah blah blah)\src\Solutions\ZzzTempOutputFiles\ToolsPath\.store\dotnet-reportgenerator-globaltool\4.5.4\dotnet-reportgenerator-globaltool\?.?.?\tools\netcoreapp?.?\any\ReportGenerator.runtimeconfig.dev.json"
REM cannot be longer than 254 (on a windows machine), or you will get "Invalid runtimeconfig.json" errors.
REM note below, we're using a wild card
call "%__toolsPath%\reportgenerator.exe" "-reports:%__testResults%%__coverageGeneratedShortNameWildCard%" -targetdir:"%__coverageResults%" -reporttypes:HTML;HTMLSummary



start "" "%__coverageResults%\index.htm"


set __dotNetExe
set __fullDirectory=
set mm=
set dd=
set yy=
set __mydate
set hh=
set mn=
set __mytime=
set __datetimeStampString=
set __outputFilesDirectory=
set __testResults=
set __coverageResults=
set __toolsPath=
set __slnShortName=
set __slnFullName=

IMPORTANT NOTE:

Your UnitTest csproj file should have/will need these references:

<PackageReference Include="coverlet.msbuild" Version="x.y.z">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> 
<PackageReference Include="coverlet.collector" Version="a.b.c">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

Mine were (at the time of writing this answer)

"coverlet.msbuild" Version="2.8.0">

"coverlet.collector" Version="1.2.1"

other useful links:

https://github.com/danielpalme/ReportGenerator