使用 MsTest 的 SonarQube 测试覆盖率
SonarQube Test Coverage with MsTest
我一直在尝试 SonarQube
使用简单的点网应用程序。我已经取得了一些成功,运行 但代码覆盖率不起作用。
当 SonarQube 停止支持许多 'go to' 覆盖工具(例如 DotCover and OpenCover
via Gallio
时,看起来很多其他人都遇到过这个问题
Examples which I have followed are:
Sonar Runner errors out during processing of .coveragexml file produced from Visual Studio's MSTest
VS2013 CodeCoverage.exe runsettings file never parses
我尝试了一些 VS 命令行工具来生成 .coverage
文件
vstest.console.exe .\UnitTestProject1\bin\Debug\UnitTestProject1.dll /EnableCodeCoverage
和
CodeCoverage.exe collect /output:DynamicCodeCoverage.coverage .\UnitTestProject1\bin\Debug\UnitTestProject1.dll
并编写了一些代码,例如将其从 here
转换为 .coveragexml
文件
得到以下XML:
<?xml version="1.0" standalone="yes"?>
<CoverageDSPriv>
<Module>
<ModuleName>unittestproject1.dll</ModuleName>
<ImageSize>32768</ImageSize>
<ImageLinkTime>0</ImageLinkTime>
<LinesCovered>12</LinesCovered>
<LinesPartiallyCovered>0</LinesPartiallyCovered>
<LinesNotCovered>0</LinesNotCovered>
<BlocksCovered>9</BlocksCovered>
<BlocksNotCovered>0</BlocksNotCovered>
<NamespaceTable>
<BlocksCovered>9</BlocksCovered>
<BlocksNotCovered>0</BlocksNotCovered>
<LinesCovered>12</LinesCovered>
<LinesNotCovered>0</LinesNotCovered>
甚至使用 XSLT 样式表提供一个可以被 SonarQube 运行器使用的样式表
<?xml version="1.0" encoding="utf-8"?>
<results>
<modules>
<module name="unittestproject1.dll" path="unittestproject1.dll" block_coverage="100" line_coverage="100" blocks_covered="9" blocks_not_covered="0" lines_covered="12" lines_partially_covered="0" lines_not_covered="0">
<functions>
<function name="Setup" type_name="UnitTest1" block_coverage="100" line_coverage="100" blocks_covered="1" blocks_not_covered="0" lines_covered="2" lines_partially_covered="0" lines_not_covered="0">
<ranges>
<range source_id="1" covered="yes" start_line="13" start_column="9" end_line="13" end_column="10" />
<range source_id="1" covered="yes" start_line="15" start_column="9" end_line="15" end_column="10" />
</ranges>
</function>
when I run Sonar
- MSBuild.SonarQube.Runner.exe Begin
- MSBuild
- MSBuild.SonarQube.Runner.exe end
I get errors like Caused By: unknown XML Node
, Expect Coverage but
got Results
这是因为它不喜欢我的 XML 的结构,但我不确定期望的是什么以及我必须对覆盖文件做多少工作才能将其转换成一种格式声纳赞
希望我走错了路,有一种简单的方法可以将 VS Coverage 或 coveragexml
文件集成到 Sonar 中而无需太多工作
Extra Information on my Sonar plugins
are
- c# = 4.1
- Generic Coverage = 1.1
C# 4.1 插件支持 OpenCover 和 dotCover 报告。分别为两个工具设置 sonar.cs.dotcover.reportsPaths
或 sonar.cs.opencover.reportsPaths
属性 以导入代码覆盖率。
Gallio 并不完全是首选工具:该项目自 2013 年以来一直处于非活动状态。依赖于 Gallio 的 SonarQube C# 插件 2.x 插件的主要问题是它自己启动 Gallio - 不是允许最终用户自定义如何启动测试和收集覆盖率。
现在情况要简单得多:启动您最喜欢的代码覆盖率工具,要求它生成报告,然后将其提供给 MSBuild SonarQube Runner。
如果您使用的是 Team Foundation Server 2013,启用代码覆盖率就像在构建定义中选择 Enable Code Coverage
选项一样。
现在,非常不幸和令人困惑的是,Microsoft 有两种不同的 .coveragexml
格式,而 SonarQube C# 插件仅支持其中一种(也就是说,目前。参见 http://jira.sonarsource.com/browse/SONARNTEST-3) .
在等待修复票证的同时,以下是生成预期 .coveragexml
报告的步骤(注意:如果您是,请在各种路径中将 14
替换为 12
使用 VS 2013 而不是 2015):
MSBuild.SonarQube.Runner begin /k:SonarQube_Project_Key /n:SonarQube_Project_Name /v:1.0 /d:sonar.cs.vscoveragexml.reportsPaths=%CD%\VisualStudio.coveragexml
msbuild
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" collect /output:VisualStudio.coverage "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "UnitTestProject1\bin\Debug\UnitTestProject1.dll"
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:VisualStudio.coveragexml VisualStudio.coverage
MSBuild.SonarQube.Runner end
我不建议使用 XSLT 转换代码覆盖率报告格式,请改用 CodeCoverage.exe
Microsoft 工具。
请确保以下事项:
1) 你有构建过程生成的覆盖率文件吗?
如果未生成,则可能需要更新构建脚本以生成一个,或者需要在构建自动化工具中添加一个明确的步骤来生成它。
For e.g. "C:\Program Files\dotnet\dotnet.exe" test <Target-filename>.csproj --logger:trx --collect:"Code Coverage"
2) 确保生成 xml 版本的 CodeCoverage 文件。
所以会有两个文件:
CodeCoverage (generated using dotnet command)
CodeCoverageXml (This is generated by using "CodeCoverage.exe analyze /output: newfilename Your_CodeCoverage_file" )
3)调用SonarQube时是否提供了正确的Coveragefile路径?
4) 您的构建服务器是否安装了正确版本的 'Dotnet' 并确保执行相同版本的 dotnet?
有时构建服务器有多个版本的 Dotnet(例如 v2.0.3 和 v2.1.0)
要生成覆盖率报告,使用正确的版本非常重要,否则它将显示为零或不会自行生成覆盖率文件。
我一直在尝试 SonarQube
使用简单的点网应用程序。我已经取得了一些成功,运行 但代码覆盖率不起作用。
当 SonarQube 停止支持许多 'go to' 覆盖工具(例如 DotCover and OpenCover
via Gallio
Examples which I have followed are:
Sonar Runner errors out during processing of .coveragexml file produced from Visual Studio's MSTest
VS2013 CodeCoverage.exe runsettings file never parses
我尝试了一些 VS 命令行工具来生成 .coverage
文件
vstest.console.exe .\UnitTestProject1\bin\Debug\UnitTestProject1.dll /EnableCodeCoverage
和
CodeCoverage.exe collect /output:DynamicCodeCoverage.coverage .\UnitTestProject1\bin\Debug\UnitTestProject1.dll
并编写了一些代码,例如将其从 here
转换为.coveragexml
文件
得到以下XML:
<?xml version="1.0" standalone="yes"?>
<CoverageDSPriv>
<Module>
<ModuleName>unittestproject1.dll</ModuleName>
<ImageSize>32768</ImageSize>
<ImageLinkTime>0</ImageLinkTime>
<LinesCovered>12</LinesCovered>
<LinesPartiallyCovered>0</LinesPartiallyCovered>
<LinesNotCovered>0</LinesNotCovered>
<BlocksCovered>9</BlocksCovered>
<BlocksNotCovered>0</BlocksNotCovered>
<NamespaceTable>
<BlocksCovered>9</BlocksCovered>
<BlocksNotCovered>0</BlocksNotCovered>
<LinesCovered>12</LinesCovered>
<LinesNotCovered>0</LinesNotCovered>
甚至使用 XSLT 样式表提供一个可以被 SonarQube 运行器使用的样式表
<?xml version="1.0" encoding="utf-8"?>
<results>
<modules>
<module name="unittestproject1.dll" path="unittestproject1.dll" block_coverage="100" line_coverage="100" blocks_covered="9" blocks_not_covered="0" lines_covered="12" lines_partially_covered="0" lines_not_covered="0">
<functions>
<function name="Setup" type_name="UnitTest1" block_coverage="100" line_coverage="100" blocks_covered="1" blocks_not_covered="0" lines_covered="2" lines_partially_covered="0" lines_not_covered="0">
<ranges>
<range source_id="1" covered="yes" start_line="13" start_column="9" end_line="13" end_column="10" />
<range source_id="1" covered="yes" start_line="15" start_column="9" end_line="15" end_column="10" />
</ranges>
</function>
when I run Sonar
- MSBuild.SonarQube.Runner.exe Begin
- MSBuild
- MSBuild.SonarQube.Runner.exe end
I get errors like Caused By:
unknown XML Node
, Expect Coverage but got Results
这是因为它不喜欢我的 XML 的结构,但我不确定期望的是什么以及我必须对覆盖文件做多少工作才能将其转换成一种格式声纳赞
希望我走错了路,有一种简单的方法可以将 VS Coverage 或 coveragexml
文件集成到 Sonar 中而无需太多工作
Extra Information on my
Sonar plugins
are
- c# = 4.1
- Generic Coverage = 1.1
C# 4.1 插件支持 OpenCover 和 dotCover 报告。分别为两个工具设置 sonar.cs.dotcover.reportsPaths
或 sonar.cs.opencover.reportsPaths
属性 以导入代码覆盖率。
Gallio 并不完全是首选工具:该项目自 2013 年以来一直处于非活动状态。依赖于 Gallio 的 SonarQube C# 插件 2.x 插件的主要问题是它自己启动 Gallio - 不是允许最终用户自定义如何启动测试和收集覆盖率。
现在情况要简单得多:启动您最喜欢的代码覆盖率工具,要求它生成报告,然后将其提供给 MSBuild SonarQube Runner。
如果您使用的是 Team Foundation Server 2013,启用代码覆盖率就像在构建定义中选择 Enable Code Coverage
选项一样。
现在,非常不幸和令人困惑的是,Microsoft 有两种不同的 .coveragexml
格式,而 SonarQube C# 插件仅支持其中一种(也就是说,目前。参见 http://jira.sonarsource.com/browse/SONARNTEST-3) .
在等待修复票证的同时,以下是生成预期 .coveragexml
报告的步骤(注意:如果您是,请在各种路径中将 14
替换为 12
使用 VS 2013 而不是 2015):
MSBuild.SonarQube.Runner begin /k:SonarQube_Project_Key /n:SonarQube_Project_Name /v:1.0 /d:sonar.cs.vscoveragexml.reportsPaths=%CD%\VisualStudio.coveragexml
msbuild
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" collect /output:VisualStudio.coverage "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "UnitTestProject1\bin\Debug\UnitTestProject1.dll"
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:VisualStudio.coveragexml VisualStudio.coverage
MSBuild.SonarQube.Runner end
我不建议使用 XSLT 转换代码覆盖率报告格式,请改用 CodeCoverage.exe
Microsoft 工具。
请确保以下事项:
1) 你有构建过程生成的覆盖率文件吗?
如果未生成,则可能需要更新构建脚本以生成一个,或者需要在构建自动化工具中添加一个明确的步骤来生成它。
For e.g. "C:\Program Files\dotnet\dotnet.exe" test <Target-filename>.csproj --logger:trx --collect:"Code Coverage"
2) 确保生成 xml 版本的 CodeCoverage 文件。
所以会有两个文件:
CodeCoverage (generated using dotnet command)
CodeCoverageXml (This is generated by using "CodeCoverage.exe analyze /output: newfilename Your_CodeCoverage_file" )
3)调用SonarQube时是否提供了正确的Coveragefile路径?
4) 您的构建服务器是否安装了正确版本的 'Dotnet' 并确保执行相同版本的 dotnet?
有时构建服务器有多个版本的 Dotnet(例如 v2.0.3 和 v2.1.0)
要生成覆盖率报告,使用正确的版本非常重要,否则它将显示为零或不会自行生成覆盖率文件。