11

I have been trying to get SonarQube working with a simple dot net app. I have had some success getting it up and running but code coverage is not working.

It looks like many other people have faced this issue when SonarQube discontinued support for many of the 'go to' coverage tool such as DotCover and OpenCover via Gallio

Examples which I have followed are:

I have tried a few of the VS command line tools to generate a .coverage file

vstest.console.exe .\UnitTestProject1\bin\Debug\UnitTestProject1.dll /EnableCodeCoverage

and

CodeCoverage.exe collect /output:DynamicCodeCoverage.coverage .\UnitTestProject1\bin\Debug\UnitTestProject1.dll

And written some code like to covert it into a .coveragexml file from here

To get the following 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>

And even used XSLT stylesheet provide in one that could be used by the SonarQube runner

<?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

  1. MSBuild.SonarQube.Runner.exe Begin
  2. MSBuild
  3. MSBuild.SonarQube.Runner.exe end

I get errors like Caused By: unknown XML Node, Expect Coverage but got Results

This is because its does not like the structure of my XML, but I am not sure what is is expecting and how much work I have to do on the coverage file to convert it into a format that Sonar likes

Hopefully I have been going down the wrong path and there is a simple way to integrate VS Coverage or coveragexml files into Sonar without too much work

Extra Information on my Sonar plugins are

  1. c# = 4.1
  2. Generic Coverage = 1.1
4

2 回答 2

7

C# 4.1 插件支持 OpenCover 和 dotCover 报告。分别为这两个工具设置sonar.cs.dotcover.reportsPathsorsonar.cs.opencover.reportsPaths属性以导入代码覆盖率。

Gallio 并不完全是首选工具:该项目自 2013 年以来一直处于非活动状态。依赖于 Gallio 的 SonarQube C# Plugin 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报告的步骤(注意:如果您使用的是 VS 2013 而不是 2015,请在各种路径中替换为)1412

  1. MSBuild.SonarQube.Runner begin /k:SonarQube_Project_Key /n:SonarQube_Project_Name /v:1.0 /d:sonar.cs.vscoveragexml.reportsPaths=%CD%\VisualStudio.coveragexml
  2. msbuild
  3. "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"
  4. "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:VisualStudio.coveragexml VisualStudio.coverage
  5. MSBuild.SonarQube.Runner end

我不建议使用 XSLT 来转换代码覆盖率报告格式,CodeCoverage.exe而是使用 Microsoft 工具。

于 2015-08-04T13:33:07.377 回答
0

请确保以下事项:

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)

要生成覆盖率报告,使用正确的版本非常重要,否则它将显示为零或不会生成覆盖率文件本身。

于 2020-02-11T10:25:33.743 回答