编辑:在 OP 为更清晰添加注释后,以下块对于理解问题是必要的:
来自下面链接的文档。
We support:
Import of coverage reports from VSTest, dotCover, OpenCover, Coverlet and NCover 3.
Import of unit test results from VSTest, NUnit and xUnit.
要包含测试结果和测试覆盖率,您需要创建将由 SonarQube 读取的 XML 报告。 Official documentation can be found here.
因为MS forum 这么说:
Visual Studio includes the VSTest and MSTest command-line tools for testing purposes.
We can use both VSTEST and MSTEST to run automated unit and coded UI tests from a command line.
我的建议是使用 VStest,因为不支持 MStest。另外,也有一些需要注意的案例,请查看文档。
运行单元测试以收集代码覆盖率
"%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /EnableCodeCoverage "UnitTestProject1\bin\Debug\UnitTestProject1.dll"
将代码覆盖率报告从二进制转换为 XML
"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output: "%CD%\VisualStudio.coveragexml" "%CD%\VisualStudio.coverage"
如果有人使用 NUnit,以下是包含它的步骤:
运行单元测试并将结果保存在文件“NUnitResults.xml”中
packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console.exe --result=NUnitResults.xml "NUnitTestProject1\bin\Debug\NUnitTestProject1.dll"
导入单元测试结果
要导入测试执行报告,在开始步骤中,您需要传递一个指向将要生成的文件的参数:
-
sonar.cs.nunit.reportsPaths 用于 NUnit
完整的命令类似于:
dotnet sonarscanner begin /k:PROJECT_NAME /d:sonar.login=LOGIN_TOKEN /d:sonar.host.url=URL /d:sonar.cs.nunit.reportsPath=PATH_TO_NUnitResults.xml