简体   繁体   中英

How can I include test-coverage reports in my C# SonarQube project?

I currently have a build file that results in basic-analysis report on my SonarQube server.

MSBuild.SonarQube.Runner.exe begin /k:"mykey" /n:"myname" /v:"1.0.0"
msbuild ... # build production code
MSBuild.SonarQube.Runner.exe end
msbuild ... # build test code
OpenCover ... # run test code and generate a (non-SonarQube) coverage report

So far so good. Now I would like to add test-coverage reports to the mix. This is where I am stuck. How can I include test-coverage reports in my C# SonarQube project?

I have tried two ways to get test-coverage reports, but neither have the desired effect:

  1. run two separate MSBuild.SonarQube.Runner.exe sessions, one for analyzing the production code and another for handling the test-coverage report; with this approach I see no errors, see basic analysis, but see no test-coverage reports:

    \nMSBuild.SonarQube.Runner.exe begin /k:"mykey" /n:"myname" /v:"1.0.0" \nmsbuild ... # build production code \nMSBuild.SonarQube.Runner.exe end \nMSBuild.SonarQube.Runner.exe begin /k:"mykey" /n:"myname" /v:"1.0.0" /d:sonar.cs.opencover.reportsPaths="opencover.xml" /d:sonar.cs.nunit.reportsPaths="nunit.xml" \nmsbuild ... # build test code \nOpenCover ... # run test code and generate a (non-SonarQube) coverage report \nMSBuild.SonarQube.Runner.exe end \n
  2. expand the original MSBuild.SonarQube.Runner.exe session to include the test-related build code; with this approach I see no errors, but also see NO reports:

    \nMSBuild.SonarQube.Runner.exe begin /k:"mykey" /n:"myname" /v:"1.0.0" /d:sonar.cs.opencover.reportsPaths="opencover.xml" /d:sonar.cs.nunit.reportsPaths="nunit.xml" \nmsbuild ... # build production code \nmsbuild ... # build test code \nOpenCover ... # run test code and generate a (non-SonarQube) coverage report \nMSBuild.SonarQube.Runner.exe end \n

Technical background

  • SonarQube 5.1.2
  • C# Plugin 5.2
  • MSBuild.SonarQube.Runner 1.0.1
  • NUnit.Runners.Net45.x64 2.6.3
  • OpenCover 4.5.3207

My background

Other background

  • I separate my production code and test code in two separate FS trees with two separate .csproj files.
  • I identify my test project, which has a MyTests.csproj project file, to SonarQube as a test project in two ways:

    1. I set sonar.cs.msbuild.testProjectPattern to [^\\\\]*Test[^\\\\]*$ on the SonarQube server and
    2. I add <SonarQubeTestProject>true</SonarQubeTestProject> to the unconditional PropertyGroup in MyTests.csproj.

The first approach--run MSBuild.SonarQube.Runner.exe twice, once around the build and once around the testing--works; the second approach does not.

The key here is to provide MSBuild.SonarQube.Runner.exe with correct paths to the test-report XML files. I wast providing it with incorrect paths, and that doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM