I'm using SonarQube and Jenkins. In Jenkins, I check the unit tests with nUnit and the coverage with opencover. The opencover's report is displayed in Sonar but the nunit's report is not.
I followed the instructions at : Analysing with SonarQube Scanner for Jenkins and at : Unit Test Execution Results Import
My "Begin Analysis and SonarQube for MSBuild" args :
/d:sonar.sources="Foo.Bar.Business, Foo.Bar.Data, Foo.Bar.Web, Foo.Bar.FileGen, Foo.Bar.FileInt, Foo.Bar.Revision"
/d:sonar.binaries="Build/Foo.Bar.Business.dll, Build/Foo.Bar.Data.dll, Build/Foo.Bar.Web.dll, Build/Foo.Bar.FileGen.exe, Build/Foo.Bar.FileInt.exe, Build/Foo.Bar.Revision.exe, Build/Foo.Bar.TableauDeBord.exe"
/d:sonar.exclusions="Foo.Bar.Web/Scripts/JQuery/**/*, Foo.Bar.Web/Scripts/MomentJS/**/*, Foo.Bar.Web/Scripts/CanvasJS/**/*, Foo.Bar.Web/Scripts/JQueryUi/**/*, Foo.Bar.Web/Scripts/DataTables/**/*, Foo.Bar.Web/Content/**/*, Foo.Bar.Web/App_Start/**/*, Foo.Bar.Data/Model/**/*, Model/**/*, **/AssemblyInfo.cs, **/*Test.cs, Foo.Bar.Test.Web/**/*, Foo.Bar.JeuTest/**/*, Foo.Bar.Gestion.Web/**/*, **/*.js"
/d:sonar.tests="Foo.Bar.Test"
/d:sonar.cs.opencover.reportsPaths="D:/Program Files (x86)/Jenkins/workspace/Bar/reports/opencovertests.xml"
/d:sonar.cs.nunit.reportsPaths="D:/Program Files (x86)/Jenkins/workspace/Bar/reports/TestResult.xml"
My "after msbuild"'s command (windows) :
MKDIR "%WORKSPACE%\reports"
MKDIR "%WORKSPACE%\reports-history"
SET COV_PTH="D:\Programmes\opencover.4.6.519"
SET TOOL_PATH="D:\Programmes\NUnit-3.4.1"
SET RPT_PATH="D:\Programmes\ReportGenerator"
%COV_PTH%\OpenCover.Console.exe -target:"%TOOL_PATH%\bin\nunit3-console.exe" -targetargs:"Build\Foo.Bar.Test.dll -result:reports\TestResult.xml;format=nunit2" -filter:"+[Foo.Bar.*]* -[Foo.Bar.Test]* -[Foo.Bar.Data]Foo.Bar.Data.Model.*" -register -output:"reports\opencovertests.xml"
%RPT_PATH%\ReportGenerator.exe "-reports:reports\opencovertests.xml" "-targetdir:reports-ReportGenerator" "-historydir:reports-history"
Versions :
Jenkins : 2.74
SonarQube : 6.5
SonarQube scanner for MSBuild :3.0.2.656
SonarQube Scanner : 3.0.3.778
SonarC# plugin : 6.4.1
OpenCover : 4.6.519
nUnit : 3.4.1
MSBuild : 14
When calling OpenCover.Console.exe you need to use parameter+value:
-register:user instead of -register only.
In same command line, be sure to be located in folder D:/Program Files (x86)/Jenkins/workspace/Bar/, where the report is stored finally.
Related
I have a dotnet image that is used as an agent for the Jenkins pipeline. Now I want to include sonar scanner in the image so that I can run an analysis and see if the coverage is good. If the coverage is not good then the build should fail. How to include the sonar scanner in my image.
I tried including the skilldlabs/sonar-scanner in my Dockerfile of the dotnet image. but when I run the container it directly executed the sonar cube commands and failed as the default sonarqube address is used.
Below is my current Dockerfile
FROM microsoft/dotnet:2.1-sdk
FROM skilldlabs/sonar-scanner:3.3
COPY some-ca.crt /usr/local/share/ca-certificates
COPY NuGet.Config /build/.nuget/NuGet/
VOLUME [ "/build/sources" ]
WORKDIR /build/sources
When I ran :
docker run --name sonar -it sonar
INFO: Scanner configuration file: /root/sonar-scanner-3.3.0.1492-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 3.3.0.1492
INFO: Java 1.8.0_191 Oracle Corporation (64-bit)
INFO: Linux 4.9.125-linuxkit amd64
INFO: User cache: /root/.sonar/cache
ERROR: SonarQube server [http://sonarqube:9000] can not be reached
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 5.433s
INFO: Final Memory: 3M/39M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: Unable to execute SonarQube
ERROR: Caused by: Fail to get bootstrap index from server
ERROR: Caused by: sonarqube: Try again
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug
logging.
How can I tell the container to provide the config for the sonar-scanner?
Instead of using the sonar scanner image into my image, I have installed dotnet-sonarscanner using the below command,
dotnet tool install -g dotnet-sonarscanner
I had to install a "coverlet" package to my unit test project by adding the below to my .csproj file of the unit test project.
<PackageReference Include="coverlet.msbuild" Version="2.6.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Now, whenever I want to send sonarqube my coverage results I run below command to generate the coverage file.
dotnet test ./UnitTests/UnitTests.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
The above command will generate the coverage.opencover.xml file in the project folder.
Now use below commands to send the coverage
dotnet sonarscanner begin /k:"yourprojectkey" /d:sonar.host.url=https://yoursonarqubedomain.com /d:sonar.cs.opencover.reportsPaths="./UnitTests/coverage.opencover.xml" /d:sonar.coverage.exclusions="**Tests*.cs"
dotnet build
dotnet sonarscanner end
you can set the sonarscanner properties like report location and the URL etc., using /d:
we are getting ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.projectName, sonar.projectVersion, sonar.sources in when executing a job in Jenkins
Installed SonarQube Scanner plugin version 2.8.1
In Jenkins job configuration Build section,Added Execute SonarQube Scnanner with analysis properties
-Dsonar.projectKey
-Dsonar.projectName
-Dsonar.projectVersion
-Dsonar.sourceEncoding
-Dsonar.jacoco.itReportPath
-Dsonar.analysis.mode
-Dsonar.github.repository
-Dsonar.host.url
-Dsonar.login
-Dsonar.password
-Dsonar.github.oauth
-Dsonar.github.pullRequest
-Dsonar.github.disableInlineComments
-Dsonar.github.endpoint
This is a java project.
we tried the same with sonarQube scanner version 2.1, it is working fine.
We are facing issue with sonarQube Scanner version 2.8.1, how to resolve this issue.
We are currently using Sonarqube 4.5.7 (SonarQube Scanner for MSBuild 2.1) with Nunit and Opencover. Test execution is successful and metrics correctly reported in dashboard. But coverage section in Sonar dashboard is blank and coverage xml shows "Module skippedDueTo="MissingPdb"" for my application module
Below is the configuration I have used in my Jenkins
"C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe" sample\app.sln /t:Rebuild /p:Configuration=Debug
"C:\Program Files (x86)\OpenCover\OpenCover.Console.exe" -register:user -targetdir:"…\sample\app.Tests\app.Correction\bin\Debug" -target:"C:\Program Files (x86)\NUnit 2.6.4\bin\nunit-console.exe" -targetargs:" …\sample\app.Tests\app.Correction\bin\Debug \app.dll /nologo /noshadow" -output:coverage.report.xml
Finally we found solution for this problem. In this case it was indeed a problem with quotes, wrapping the complete targetargs, as shown below fixed the problem
"-targetargs: …\sample\app.Tests\app.Correction\bin\Debug \app.dll /nologo /noshadow"
I'm working on a web application using Grails 2.1.1 and it was requested me to make evidence about the test code coverage.
The project is using:
Maven 3.0.5
code-coverage 1.2.7 (grails plugin for Cobertura)
sonar-maven-plugin 2.5
SonarQube 3.7.4 (running on localhost)
On my pom.xml, there are declared the following sonar's properties:
<sonar.language>grvy</sonar.language>
<sonar.groovy.cobertura.reportPath>target/test-reports/cobertura/coverage.xml</sonar.groovy.cobertura.reportPath>
I successfully generate cobertura.xml report inside directory target/test-reports/cobertura, launching the command:
grails tA -coverage -xml
but when I run:
mvn sonar:sonar
It isn't displayed any test report on Sonar, although I receive as command response the following output lines:
[INFO] [15:20:42.007] Sensor Groovy CoberturaSensor...
[INFO] [15:20:42.007] Analyzing Cobertura report: target/test-reports/cobertura/coverage.xml
[INFO] [15:20:42.086] Sensor Groovy CoberturaSensor done: 79 ms
I am using SonarQube Runner 2.4 at jenkins since today but the analysis fail with the message:
SonarQube Runner 2.4
Java 1.7.0_51 Oracle Corporation (64-bit)
INFO: Runner configuration file: /.jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Sonar_Runner_2.4/conf/sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "en_US", source code encoding: "ISO-8859-1" (analysis is platform dependent)
INFO: Work directory: /.jenkins/workspace/Sonar/.sonar
INFO: SonarQube Server 4.3.2
23:21:42.176 INFO - Load batch settings
23:21:42.160 INFO - User cache: /.sonar/cache
23:21:42.270 INFO - Install plugins
23:21:43.322 INFO - Install JDBC driver
23:21:43.329 INFO - Create JDBC datasource for jdbc:mysql://xxx
23:21:45.505 INFO - Initializing Hibernate
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 5.214s
Final Memory: 21M/429M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: You must define the following mandatory properties for ‚com.mypackage:MyProject: sonar.sources
ERROR:
ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.
ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
Build step 'Invoke Standalone Sonar Analysis' marked build as failure
[BFA] Scanning build for known causes...
[BFA] Done. 0s
Notifying upstream projects of job completion
Finished: FAILURE
If I add the property "MyProject.sonar.sources=src" In the project properties in Jenkins (I use "Invoke Standalone Sonar Analysis"). I will receive the error message
ERROR: Caused by: Findbugs needs sources to be compiled. Please build project before executing sonar and check the location of compiled classes.
This means the sonar-project.properties (/.jenkins/workspace/Deploy/MyProject/sonar-project.properties) is not read or used because there are all values defined:
sonar.language=java
sonar.projectName=MyProject
sonar.projectVersion=0.1
sonar.binaries=bin
sonar.projectDescription=
sonar.projectKey=MyProjectKey
sonar.sources=src
When sonar is executed I can see the correct path to MyProject where :
-DMyProject.sonar.projectBaseDir=/.jenkins/workspace/Deploy/MyProject
Or does the name of sonar-project.properties changed?
Please build the project in the release mode. After the build happens you will get the dll's produced somewhere. you have to point to that location.