【问题标题】:Sonarqube showing code coverage as 0%, but shows the no of tests passedSonarqube 将代码覆盖率显示为 0%,但显示未通过测试
【发布时间】:2020-02-25 09:28:34
【问题描述】:

我已经开始使用 Sonarqube,并且我已经设置了一个本地声纳服务器来测试它是如何工作的。

之前我使用/d:sonar.cs.vscoveragexml.reportsPaths 并生成了.coveragexml 文件。现在我正在尝试使用MSTest 命令生成.trx 文件。
所以这些是我用来运行声纳分析的命令。

MSBuild.SonarQube.Runner.exe begin /k:"93ca937be91ab25536462fgdfg566915" /n:"Solution" /v:"1" /d:sonar.cs.vstest.reportsPaths="C:\SonarQube\Solution.trx"

MSBuild.exe "Solution.sln" /t:Rebuild /p:Configuration=Release

MSTest /testcontainer:.\SolutionTests\bin\Release\SolutionTests.dll /resultsfile:"C:\SonarQube\Solution.trx"

MSBuild.SonarQube.Runner.exe end

在命令提示符下运行所有​​这些命令后,代码覆盖率显示为 0%,测试运行次数显示为 22。

我是否缺少任何其他命令来获取代码覆盖率。我知道有一个类似下面的命令:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\TestAgent\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:"C:\SonarQube\Solution.trx"

我找不到分析 .trx 文件的确切命令。如果有人可以在这件事上提供帮助,那将非常有帮助。非常感谢。

【问题讨论】:

  • 请注意,Sonar Cloud 仅测量新添加的代码行的代码覆盖率。

标签: c# sonarqube code-coverage mstest trx


【解决方案1】:

SonarQube 文档不能很好地解释所有功能。

你需要做两件事-

一个是使用 trx 文件执行单元测试用例,该文件将显示单元测试用例的数量。
其次,在分析代码覆盖率时,需要分析 CodeCoverage.exe 生成的 .coveragexml 文件。为了解释这一点——一旦执行单元测试,.trx 文件就是记录器文件,.coverage 文件有覆盖数据。当 CodeCoverage.exe 分析此 .coverage 文件时,它会生成 .coveragexml 文件,我们需要将其传递给声纳扫描仪进行 msbuild 分析和生成报告。 因此,您的命令行将如下所示-

MSBuild.SonarQube.Runner.exe begin /k:"93ca937be91ab25536462fgdfg566915" /n:"Solution" /v:"1" /d:sonar.cs.vstest.reportsPaths="C:\SonarQube\*.trx" /d:sonar.cs.vscoveragexml.reportsPaths="C:\SonarQube\*.coveragexml"

MSBuild.exe "Solution.sln" /t:Rebuild /p:Configuration=Release

//now you should try to run test cases and get .coverage file. I am using vstest. Please check for your vstest.console.exe path as per your Visual Studio installation

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /EnableCodeCoverage "%CD%\SolutionTests\bin\Release\SolutionTests.dll" /ResultsDirectory:"%CD%" /Logger:trx
 
//now as .coverage file is there, we will analyze it and generate .coveragexml using CodeCoverage.exe

"C:\Program Files (x86)\Microsoft Visual Studio\2017\TestAgent\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:"C:\SonarQube\VSCoverage.coveragexml" "<give here path for .coverage file"

MSBuild.SonarQube.Runner.exe end

【讨论】:

  • 您不必删除和添加。你可以edit你现有的帖子。
  • 实际上我在 VS 中得到的是 .coverage(不是 .coveragexml)并且没有 trx,有什么提示我缺少什么吗?
猜你喜欢
  • 2018-02-15
  • 2020-08-07
  • 2021-03-01
  • 2016-04-08
  • 1970-01-01
  • 2019-07-07
  • 2020-08-13
  • 2017-10-24
  • 1970-01-01
相关资源
最近更新 更多