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.
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.
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
Mid Year 2021 Update/Append (for linux)
Here is a linux .sh version of the above. Much cleaner.
Obviously, my filename is "zzzCoverage.sh"
I put the file "next" to my .sln file. Watch the "..", I'll explain my folder structure after the .sh contents.
#!/bin/bash
# don't forget to allow execution of this file
# chmod +x zzzCoverage.sh
# READ THIS --> this has a prerequisite step of installing the below global dotnet tool
# dotnet tool install -g dotnet-reportgenerator-globaltool
# delete any existing coverage.cobertura.xml files (to ensure clean coverage report)
# note, if you target more than 1 framework in your UnitTests.csprojs, you may get multiple file names like "coverage.net5.0.cobertura.xml" AND "coverage.netstandard2.1.cobertura.xml". In this case, you will have to adjust the "wildcard" value for finding one set of the cobertura.xml files
find .. -type f -name 'coverage.cobertura.xml' -delete
# run dotnet test. Note the arguments. And see the PackageReferences below
dotnet test --logger "trx;logfilename=TestResults123.trx" --results-directory ./BuildReports/UnitTests /p:CollectCoverage=true /p:CoverletOutput=BuildReports/Coverage/ /p:CoverletOutputFormat=cobertura
# generate the report with the global dotnet tool. note the prerequisite above
# note the -reports argument. You may have to play with it a little. note the "coverage.net5.0.cobertura.xml coverage.netstandard2.1.cobertura.xml" issue earlier in this file as well
reportgenerator -reports:../**/coverage.cobertura.xml -targetdir:BuildReports/Coverage -reporttypes:"HTML;HTMLSummary"
#open html root index.html file in a Chrome
open -a "Google Chrome" ./BuildReports/Coverage/index.html
# change the above to open ./BuildReports/Coverage/index.html (aka remove the -a "Google Chrome") if you do not have Chrome installed.
# Here is the list of packages (at the time of writing) in EACH UnitTest.csproj. Note There are TWO <ItemGroup>'s.
# <ItemGroup>
# <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
# <PackageReference Include="MSTest.TestAdapter" Version="2.2.5" />
# <PackageReference Include="MSTest.TestFramework" Version="2.2.5" />
# <PackageReference Include="coverlet.collector" Version="3.1.0">
# <PrivateAssets>all</PrivateAssets>
# <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
# </PackageReference>
# </ItemGroup>
# I believe the below PackageReferences are the ones that allow the 'reportgenerator' to function correctly. "coverlet.collector" (from above) may also be a requirement for 'reportgenerator'
# <ItemGroup>
# <PackageReference Include="coverlet.msbuild" Version="3.1.0">
# <PrivateAssets>all</PrivateAssets>
# <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
# </PackageReference>
# <PackageReference Include="Microsoft.CodeCoverage" Version="16.10.0" />
# <PackageReference Include="ReportGenerator" Version="4.8.12" />
# </ItemGroup>
Ok, my folder setup.
./src/
./src/MyCsprojProjectOne/mycsprojone.csproj
./src/MyCsprojProjectTwo/mycsprojtwo.csproj
./src/UnitTestsHolder/UnitTestLibOne/myunittestone.csproj
./src/UnitTestsHolder/UnitTestLibTwo/myunittesttwo.csproj
./src/Solutions/MySolution.sln
Key Words
DotNet Core Coverage Reports Linux Multiple CsProj CsProjs cs projects AspNet Core AspNetCore
Links:
Is there anyway to merge cobertura coverage xml reports together?