如何在donetcore 2应用程序中获取代码覆盖率报告

时间:2018-03-01 11:44:49

标签: code-coverage .net-core-2.0

我是dotnet core 2.0的新手,不知道如何使用

提取代码覆盖率报告

dotnet test命令。

寻求社区的帮助。

2 个答案:

答案 0 :(得分:1)

OpenCover是dotnet核心的开源代码覆盖工具。您可以将它与ReportGenerator结合使用来获取覆盖率报告。

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

答案 1 :(得分:0)

这是我的.bat文件,用于生成dotnet核心。

这将“弹出” index.htm文件。

这只是我在这里找到的.bat文件版本:

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

注意,我有一些“ 3.1”参考。它不会与2.x有所不同。只需将以下内容调整到您的版本/世界即可。

非常感谢,dotnet-core版本比dotnet-framework版本(在这里我对dotnet框架的回答:MSTest Code Coverage)要容易得多

下面显示了如何从1个或多个UnitTests.csproj获取结果,以及如何将html报告与所有报告进行“合并”。 (如此处所述: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=

重要说明:

您的UnitTest csproj文件应该具有/将需要以下引用:

<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>

我的是(在撰写此答案时)

“ coverlet.msbuild”版本=“ 2.8.0”>

“ coverlet.collector”版本=“ 1.2.1”

其他有用的链接:

https://github.com/danielpalme/ReportGenerator