.netsonarqubecode-coveragesonarqube-msbuild-runner

Sonar MSBuild: Getting coverage reports on Linux


I have managed to perform static code analysis for a .NET core project using SonarQube Scanner for MSBuild, on a debian:stretch docker container.

I am now trying to produce a coverage report.

Unless I am wrong, the relevant guidelines hint that you cannot just use an existing report, but rather follow a dynamic process of

a) Visual Studio Code Coverage

b) dotCover

c) OpenCover

My question is whether it is possible to run the above process on Linux (haven't managed to do so yet or find any resources)


Solution

  • Coverage reporting as also SonarQube integration is possible thanx to minicover.

    Minicover now uses mini-OpenCover to generate (and upload to a SQ server) SQ-compatible coverage reports; The procedure that should be followed more or less, scripted:

    (assuming tools is the folder that performs the nugget installation for minicover)

    echo "Starting sonarqube integration"
    mono /home/pathTo/SonarQube.Scanner.MSBuild.exe begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="myLoginId" /d:sonar.cs.opencover.reportsPaths="coverage.xml"
    
    
    dotnet restore
    dotnet build
    cd tools
    
    export "MiniCover=dotnet minicover"
    
    # Instrument assemblies inside 'MyTestFolder' folder to detect hits for source files inside 'MySrcFolder' folder
    $MiniCover instrument --workdir ../ --assemblies MyTestFolder/**/bin/**/*.dll --sources MySrcFolder/**/*.cs 
    
    # Reset hits count in case minicover was run for this project  
    $MiniCover reset
    
    cd ..    
    
    dotnet test --no-build ./MyTestFolder/MyTest.csproj
    
    cd tools    
    # Uninstrument assemblies, it's important if you're going to publish or deploy build outputs
    $MiniCover uninstrument --workdir ../
    
    # Create html reports inside folder coverage-html
    $MiniCover opencoverreport --workdir ../ --threshold 90
    
    # Print opencover report
    $MiniCover opencoverreport --workdir ../ --threshold 90
    
    cd ..
    echo "Ending sonarqube integration..."
    mono /home/pathTo/SonarQube.Scanner.MSBuild.exe end /d:sonar.login="myLoginId"
    

    More extensive discussion/details can be found on this and this threads.