How to get code coverage report in donetcore 2 application

安稳与你 提交于 2020-02-06 11:11:13

问题


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 )

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.Cool.Solution.sln
set __slnFullName=%__fullDirectory%%__slnShortName%


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 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 "%__fullDirectory%..\UnitTests\My.Cool.UnitTests.csproj" --logger:trx;LogFileName="%__testResults%My.Cool.UnitTests.trx"  /p:CollectCoverage=true /p:CoverletOutput="%__testResults%MyCoverletOutput.xml" /p:CoverletOutputFormat=cobertura


call "%__toolsPath%\reportgenerator.exe" "-reports:%__testResults%MyCoverletOutput.netcoreapp3.1.xml" -targetdir:"%__coverageResults%" -reporttypes:HTML;HTMLSummary


REM start /d "C:\Program Files\Internet Explorer" IEXPLORE.EXE "%__coverageResults%\index.htm"
REM or
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=


来源:https://stackoverflow.com/questions/49049029/how-to-get-code-coverage-report-in-donetcore-2-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!