SonarQube Test Coverage with MsTest
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 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.
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.
If you are using Team Foundation Server 2013, enabling code coverage is as choosing the Enable Code Coverage
option in the build definition.
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).
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):
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
I would not recommend to use XSLTs to convert code coverage report formats, use the CodeCoverage.exe
Microsoft tool instead.
Related videos on Youtube
user3210699
Updated on June 04, 2022Comments
-
user3210699 almost 2 years
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 GallioExamples which I have followed are:
I have tried a few of the VS command line tools to generate a
.coverage
filevstest.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 hereTo 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
- MSBuild.SonarQube.Runner.exe Begin
- MSBuild
- MSBuild.SonarQube.Runner.exe end
I get errors like Caused By:
unknown XML Node
, Expect Coverage but got ResultsThis 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 workExtra Information on my
Sonar plugins
are- c# = 4.1
- Generic Coverage = 1.1
-
user3210699 almost 9 yearsThanks for the quick repsonse! That worked perfectly. I got the code coverage imported using the CodeCoverage.exe. We do not use TFS (TeamCity is our standard) so I did not explore that option. I have not tried the OpenCover or DotCover suggested solution but will do and report back.
-
Saan over 8 years@user3210699 Hi, did you ever get the OpenCover option working with MSTest?
-
Dinesh Bolkensteyn over 8 yearsHi, I've only tried OpenCover with VSTest, but I would expect MSTest to work in a similar way. You can look at docs.sonarqube.org/display/PLUG/… for the complete instructions.
-
fluf about 8 years@Dinesh-SonarSourceTeam is it possible to add how to do this with tfs vnext build steps ?
-
Dinesh Bolkensteyn about 8 years@fluf if you're using the VSTest task, then simply enabling code coverage there should be enough. If you're running a 3rd party code coverage tool, then you can run any command using the command task.
-
Richard Schaefer almost 7 years@DineshBolkensteyn - should the above work with VS2015, Sonarqube 5.6.6 and the SonarJava 4.11.0.10660? I ran the above commands which created a coverage file of about 2mb but nothing was imported into Sonarqube.