由网友(低调的过着、流氓的生活)分享简介:我希望在本地SonarQube实例(在Windows上)中显示.NET 5单元测试的测试覆盖率。dotnet sonarscanner begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="" /d...
我希望在本地SonarQube实例(在Windows上)中显示.NET 5单元测试的测试覆盖率。
dotnet sonarscanner begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="<token>" /d:sonar.cs.opencover.reportsPaths="**TestResults**.xml"
dotnet build
dotnet test --no-build --collect:"XPlat Code Coverage"
dotnet sonarscanner end /d:sonar.login="<token>"
dotnet test
命令将覆盖率报告生成为<TestProjectDir>.TestResults<some-guid>
文件夹中的coverage.cobertura.xml
文件。
WARN: Could not import coverage report '<MyTestProject>TestResultsa4af5812-7f80-469b-8876-3ea0c7c4c98dcoverage.cobertura.xml' because 'Missing root element <CoverageSession> in C:Users<Path>TestResultsa4af5812-7f80-469b-8876-3ea0c7c4c98dcoverage.cobertura.xml at line 2'. Troubleshooting guide: https://community.sonarsource.com/t/37151
按照警告消息中的link,我可以看到只支持Visual Studio代码覆盖率、dotCover和OpenCover/overlet。据我所知,他们的GitHub page是OpenCover/Coverlet是XPlat代码覆盖(&Q;)。
在我的测试项目中,安装了coverlet.collector
NuGet Package v3.0.3。
我错过了什么?
我发现了此相关问题:SonarQube: Unable to import test coverage但它对我没有帮助,因为我无法使用dotCover。
推荐答案
经过多次试错,以下是对我有效的解决方案。
我必须安装dotnet-sonarscanner
和dotnet-reportgenerator-globaltool
的最新版本才能正常工作。我已经安装了报告生成器,但需要更新它以使用SonarQube报告类型。
dotnet tool install --global dotnet-sonarscanner --version 5.2.0
dotnet tool update dotnet-reportgenerator-globaltool -g --version 4.8.7
使用reportgenerator
可以将Cobertura文件转换为SonarQube格式。
dotnet sonarscanner begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="<token>" /d:sonar.coverageReportPaths=".sonarqubecoverageSonarQube.xml"
dotnet build
dotnet test --no-build --collect:"XPlat Code Coverage"
reportgenerator "-reports:*TestResults*coverage.cobertura.xml" "-targetdir:sonarqubecoverage" "-reporttypes:SonarQube"
dotnet sonarscanner end /d:sonar.login="<token>"
报表生成器支持的文件格式:https://github.com/danielpalme/ReportGenerator#supported-input-and-output-file-formats
SonarQube通用测试数据格式: https://docs.sonarqube.org/latest/analysis/generic-test/
相关推荐
最新文章