Link Unittest in C# and Sonar Scan in Jenkins?

Must-share information (formatted with Markdown):

  • SonarScanner for MSBuild 5.4.1
  • SonarScanner is on Windows System installed.

Hello all,

I have the following problem. I have a C# application that has some unit tests. I run these normally on my Jenkins server in the Declarative pipeline.

For this I have a stage that runs these tests first, it looks like this:

   stage('Stage 1 - UnitTest') {
            when{
                anyOf {
                    branch 'develop'
                }
            }
            steps {
                echo 'Test'
                bat 'vstest.console UnitTests.dll --logger: "trx;LogFileName=MyResults.trx" --ResultsDirectory:.'
                bat 'OpenCover.Console.exe -register:user -targetargs:"%WORKSPACE%\\UnitTests.dll" -output:\"%WORKSPACE%\\coverage.xml\"'
                bat 'OpenCoverToCoberturaConverter.exe "-input:coverage.xml" "-output:cobertura.xml"'
            }
        }

Next, I have a second stage. This will do the unit tests again and then upload them to SOnar Qube.

  stage('Stage 2 - SonarQube') {
        when{
            anyOf {
                branch 'develop'
            }
        }
        steps {
            withSonarQubeEnv('Sonarqube') {
                echo 'MSBuild'
                bat "${scannerHome}\\SonarScanner.MSBuild.exe begin /k:BuildWorld /n:BuildWorld /v:${env.BUILD_ID} /d:sonar.cfamily.threads=2 /d:sonar.c.file.suffixes=- /d:sonar.cpp.file.suffixes=- /d:sonar.objc.file.suffixes=- /d:sonar.cs.opencover.reportsPaths=%WORKSPACE%\\coverage.xml"
                bat 'vstest.console UnitTests.dll --logger: "trx;LogFileName=MyResults.trx" --ResultsDirectory:.'
                bat "${scannerHome}\\SonarScanner.MSBuild.exe end"
            }              
        }
    }

Problem: The problem is my unit tests take 20-30 minutes and if I run them twice you can imagine how long it takes.

Is there a solution to combine these two steps somehow, i.e. to create a file in the vstest that sonar scanner can import and upload ?

Why have I done this so far, you may ask.

The problem is that Jenkins does not recognize SonarQube when a test fails. So I have to start the tests freely.

Hey there.

I don’t quite understand why you run your unit tests twice, can you go into more detail? Why not run SonarQube analysis in the 1st stage?

The problem with Jenkins is that, if the unit tests fail, the Jenkins pipeline stops immediately, which I like.

With Sonar integration, Jenkins would just keep going. Also if the tests fail the stage in Jenkins would be green which would be a no go. Hence the two stages.

We generally expect that all tests should pass before SonarQube analysis is run (otherwise, you could end up with coverage numbers when you actually have failing tests!)

Exactly, and that’s the problem. then I have to run the unit tests twice.

In stage 1 and in stage 2.

I have to put in the SonarScanner.MSBuild.exe both msbuild and the unit tests or not ?

An ideal configuration looks like this.

SonarScanner.MSBuild.exe begin
[build command]
[test command]
SonarScanner.MSBuild.exe begin

With just the tests running once.

It’s not clear to me what advantage you’re expecting by splitting them out.