【发布时间】:2018-08-09 11:34:16
【问题描述】:
我是 dotnet core 2.0 的新手,不知道如何提取代码覆盖率报告
dotnet 测试命令。
向社区寻求帮助。
【问题讨论】:
标签: code-coverage .net-core-2.0
我是 dotnet core 2.0 的新手,不知道如何提取代码覆盖率报告
dotnet 测试命令。
向社区寻求帮助。
【问题讨论】:
标签: code-coverage .net-core-2.0
这是我生成 dotnet 核心的 .bat 文件。
这将“弹出”一个 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-report 成为所有这些的“合并”。 (如此处所述: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
2021 年年中更新/追加(适用于 linux)
这是上面的 linux .sh 版本。 干净多了。
显然,我的文件名是“zzzCoverage.sh”
我将文件“下一个”放在我的 .sln 文件中。 看“..”,我会在.sh内容之后解释我的文件夹结构。
#!/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>
好的,我的文件夹设置。
./src/
./src/MyCsprojProjectOne/mycsprojone.csproj
./src/MyCsprojProjectTwo/mycsprojtwo.csproj
./src/UnitTestsHolder/UnitTestLibOne/myunittestone.csproj
./src/UnitTestsHolder/UnitTestLibTwo/myunittesttwo.csproj
./src/Solutions/MySolution.sln
关键词
DotNet Core 覆盖报告 Linux 多个 CsProj CsProjs cs 项目 AspNet Core AspNetCore
链接:
Is there anyway to merge cobertura coverage xml reports together?
【讨论】:
runtimeconfig.json MAX_PATH 问题的更多信息:github.com/dotnet/BenchmarkDotNet/issues/1328