3

.NET 中的常见模式是在单独的项目中进行单元测试,这似乎可以正常工作,但是,我们正在尝试实现一种模式,其中单元测试与实现一起存在于项目中。

当尝试使用 Coverlet 生成覆盖文件时,sonarscanner 工具正在接受多个文件,但它没有报告覆盖范围:

dotnet-sonarscanner /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml"
...    

dotnet test --configuration Release \
                    --no-build \
                    --no-restore \
                    --logger "trx;LogFileName=testresults.trx" \
                    /p:CollectCoverage=true \
                    /p:CoverletOutputFormat=opencover
...

INFO: Parsing the OpenCover report /root/app/src/./Project.Data/coverage.opencover.xml
INFO: Adding this code coverage report to the cache for later reuse: /root/app/src/./Project.Data/coverage.opencover.xml
INFO: Parsing the OpenCover report /root/app/src/./Project.WebAPI/coverage.opencover.xml
INFO: Adding this code coverage report to the cache for later reuse: /root/app/src/./Project.WebAPI/coverage.opencover.xml
INFO: Parsing the OpenCover report /root/app/src/./Project.Domain/coverage.opencover.xml
INFO: Adding this code coverage report to the cache for later reuse: /root/app/src/./Project.Domain/coverage.opencover.xml
WARN: The Code Coverage report doesn't contain any coverage data for the included files. For troubleshooting hints, please refer to https://docs.sonarqube.org/x/CoBh

提供的链接在这种情况下没有显示任何帮助,并且在项目文件中添加 DebugType Full (来自相关答案)不会产生不同的结果。

4

1 回答 1

2

看看.sonarqube\out\ProjectInfo.log。这将告诉您每个项目是如何分类的。如果它被归类为测试项目,则不会上传覆盖范围。

MSBuild 的扫描器使用许多标准来确定项目是否为测试项目,例如文件中列出的项目类型指南和项目功能(请参阅此处)。如果您引用了一个著名的测试框架,那么扫描器很可能会确定整个项目是一个测试项目而不是对其进行分析。

如果是这种情况,那么您可以按如下方式解决它:

  • 通过设置 MSBuild 属性将项目明确标记为不是测试项目<SonarQubeTestProject>false</SonarQubeTestProject>。这将使扫描仪分析项目,包括项目中的测试文件。
  • 然后使用排除项来忽略测试文件,以便不对其进行分析。
于 2019-07-30T14:34:39.687 回答