具有MsTest的SonarQube测试覆盖率 [英] SonarQube Test Coverage with MsTest

查看:122
本文介绍了具有MsTest的SonarQube测试覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让SonarQube与一个简单的点网应用程序一起工作.我已经成功启动并运行了它,但是代码覆盖无法正常工作.

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.

当SonarQube停止通过

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:

VS2013 CodeCoverage.exe运行设置文件从未解析

我已经尝试了一些VS命令行工具来生成.coverage文件

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

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

并从这里

要获取以下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运行器使用的样式表

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开始
  2. MSBuild
  3. MSBuild.SonarQube.Runner.exe结束
  1. MSBuild.SonarQube.Runner.exe Begin
  2. MSBuild
  3. MSBuild.SonarQube.Runner.exe end

我收到类似原因"的错误:unknown XML Node,期望覆盖率,但是 得到了结果

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

这是因为它不喜欢XML的结构,但是我不确定期望什么以及我必须对coverage文件进行多少工作才能将其转换为Sonar喜欢的格式

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

希望我走错了路,有一种简单的方法可以将VS Coverage或coveragexml文件集成到Sonar中,而无需太多工作

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

我的Sonar plugins上的其他信息是

  1. c#= 4.1
  2. 通用覆盖率= 1.1

推荐答案

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

Both OpenCover and dotCover reports are supported by the C# 4.1 plugin. Set the sonar.cs.dotcover.reportsPaths or sonar.cs.opencover.reportsPaths property respectively for both tools to import code coverage.

Gallio并不完全是首选工具:该项目自2013年以来就一直处于闲置状态.SonarQube C#Plugin 2.x插件(主要依赖Gallio)的主要问题在于,它本身是在启动Gallio的-不允许用户可以自定义应如何启动测试和收集覆盖范围.

Gallio is not exactly the go-to tool: the project is inactive since 2013. The main issue with the SonarQube C# Plugin 2.x plugin which relied on Gallio is that it was launching Gallio by itself - not allowing the end-user to customize how tests should be launched and coverage collected.

现在情况要容易得多:启动您喜欢的代码覆盖率工具,要求它生成报告,然后将其提供给MSBuild SonarQube Runner.

Now the situation is much easier: Launch your favorite code coverage tool, ask it to produce a report, and feed it to the MSBuild SonarQube Runner.

如果使用的是Team Foundation Server 2013,则启用代码覆盖范围就像在生成定义中选择Enable Code Coverage选项一样.

If you are using Team Foundation Server 2013, enabling code coverage is as choosing the Enable Code Coverage option in the build definition.

现在,非常不幸和令人困惑的是,Microsoft有两种不同的.coveragexml格式,而SonarQube C#插件仅支持其中一种(即,现在.请参阅

Now, it is very unfortunate and confusing that Microsoft has two different .coveragexml formats, and that the SonarQube C# Plugin only supports one of them (that is, for now. see http://jira.sonarsource.com/browse/SONARNTEST-3).

在等待该票证修复的同时,以下是生成预期的.coveragexml报告的步骤(注意:如果使用VS 2013而不是2015,则在各个路径中将14替换为12):

While waiting for that ticket to be fixed, here are the steps to produce the expected .coveragexml report (note: Replace 14 by 12 in the various paths if you are using VS 2013 instead of 2015):

  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
  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工具.

I would not recommend to use XSLTs to convert code coverage report formats, use the CodeCoverage.exe Microsoft tool instead.

这篇关于具有MsTest的SonarQube测试覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆