Running sonar analysis on test code using maven - maven

I have a project which uses Maven. I wish to run Sonar on the integration test classes.
I have tried to add the pattern **/*ITCase.java in Settings / Exclusions / Test file inclusions (in the Sonar UI that's at localhost:9000). This setting is said to be
"Patterns used to include some test files and only these ones in analysis."
However, the files still are excluded. Any ideas? Has anyone used that setting successfully?

I have not found a solution with Maven.
But using the sonar runner, it is quite simple and works well.
I put a sonar-project.properties file in the src/test/java directory, apart from the sonar.projectKey, sonar.projectName and sonar.projectVersion, these are the settings to use:
sonar.sources = .
sonar.binaries = ../../../target/test-classes
sonar.language = java
sonar.sourceEncoding = UTF-8
The sonar runner can be run from the test/java directory and the project will appear in the Sonar local webpage at http://localhost:9000

Related

Can Sonar exclude files outside maven project? Also, can it exclude files other than Java?

I have a use-case where I have few image files (for my README page) outside of the maven project. I want Sonar analysis to skip performing analysis on that file. I tried keeping the file in the main project pom as below:
<sonar.exclusions>**/*.png</sonar.exclusions>
This still gives me the error:
[sonar4bitbucket] Failed to parse diff: string matching regex `\z' expected but `B' found
Any suggestions to skip .png images being allowed outside of maven project with Sonar analysis ?
you can configure exclusions in the UI:
At the global level, the navigation path is Administration > General
Settings > Analysis Scope. At the project level, the navigation path
is Project Settings > General Settings > Analysis Scope
Source: Narrowing the focus
If you want to stay with maven properties, also make sure you do not override those in the build using mvn … -Dsonar.exclusions=….
Also keep in mind that those property-basedd exclusions are not being stored in the Sonar DB. Therefore you might see an old coverage check jumping in which you might want to exclude as well:
<sonar.coverage.exclusions>
**/*.gif,
**/*.png
</sonar.coverage.exclusions>

File 'someFile.java' is ignored. It is not located in project basedir 'D:\HybrisSuites\HYBRISCOMM6400P\hybris\bin\platform'

Recently we were migrated SonarQube-6.3 to SonarQube7.9. From the Hybris side we are using sonar.xml is ant build file.
When we ran the sonar via ant command is ant sonar
we are getting the below issues.
[sonar:sonar] File 'someFile.java' is ignored. It is not located in project basedir 'D:\HybrisSuites\HYBRISCOMM6400P\hybris\bin\platform'.
based on the sonar.xml, we are mentioned the sonar properties in local.properties file.
sonar.projectName=TE
sonar.projectKey=TE
sonar.projectVersion=1.0
sonar.verbose=true
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.scm.disabled=True
sonar.host.url=http://localhost:8000
sonar.java.source=1.8 sonar.excludedExtensions=core,testweb,b2bacceleratoraddon,hystrixaddon,oauth2,embeddedserver,tomcatembeddedserver,platformbackoffice,voucherbackoffice,backofficesolrsearch,pcmbackoffice,promotionsbackoffice,ticketsystembackoffice,adaptivesearch,ordercalculation,webservicescommons,solrfacetsearchbackoffice,basecommercebackoffice,ruleengine,ruleengineservices,ruledefinitions,ruleenginebackoffice,b2bcommercefacades,b2bapprovalprocessfacades,droolsruleengineservices,promotionengineservices,promotionenginebackoffice,couponservices,couponwebservices,couponfacades,couponbackoffice,rulebuilderbackoffice,solrserver,commerceservicesbackoffice,customersupportbackoffice,adaptivesearchbackoffice,adaptivesearchsolr,warehousing,commerceorgaddon,paymetric,scripting,paymentstandard,mediaweb,maintenanceweb,deliveryzone,commons,processing,impex,validation,catalog,europe1,platformservices,workflow,hac,comments,advancedsavedquery,springintegrationlibs,ldap,hmc,virtualjdbc,cockpit,admincockpit,reportcockpit,platformhmc,productcockpit,customerreview,sapcoretest,sapcoreodata,sapcore,sapcorejco,sapcorejcorec,sapcorebol,advancedexport,backoffice,datahubbackoffice,mcc,wishlist,mediaconversion,solrfacetsearch,solrfacetsearchhmc,voucher,promotions,basecommerce,ticketsystem,cms2,cms2lib,btg,cmscockpit,btgcockpit,b2bcommerce,payment,commerceservices,b2bapprovalprocess,commercewebservicescommons,cscockpit,acceleratorservices,b2bacceleratorservices,acceleratorcms,commercefacades,acceleratorfacades,acceleratorstorefrontcommons,b2bacceleratorfacades,addonsupport,sapcoreaddon,captchaaddon,liveeditaddon,commercesearch,commercesearchhmc,commercesearchbackoffice,datahubadapter,sapcoreconfiguration,sapmodel,sapproductavailability,sapproductavailabilityhmc,b2bsapproductavailability,sappricingbol,sappricinghmc,sappricing,sappricingproductcarouseladdon,b2bsappricing,sapcustomerb2c,sapcustomerb2b,sapmodelhmc,saporderexchange,saporderexchangeb2b,sapcoreconfigurationhmc,sapproducthmc,sapcoreconfigurationbackoffice,multipaymentmode,amazonpay,codpayment,bspay,paypalservice,acceleratorcmshmc,warehousingwebservices,npmancillary,acceleratorwebservicesaddon,commerceserviceshmc,yaddon,customerticketingfacades,consignmenttrackingservices,ordermanagementwebservices,ordermanagementaddon,sap,promotionenginesamplesaddon,cmsfacades,consignmenttrackingfacades,ordermanagementfacade,warehousingfacade,samlsinglesignon,ycommercewebservices,consignmenttrackingaddon,ycommercewebservices,ycommercewebserviceshmc,acceleratorserviceshmc,customerticketingaddon,orderselfserviceaddon,asynchronousOM,saporderexchangehmc,saporderexchangebackoffice
sonar.global.exclusions= **/gensrc/**/*.java ,**/mobile/**/*.java,**/v1/**/*.java
sonar.exclusions=**/gensrc/**/*.java ,**/mobile/**/*.java,**/v1/**/*.java
sonar.sources=D:/HybrisSuites/HYBRISCOMM6400P/hybris/bin/custom/te/tecore/src
sonar.java.binaries=D:/HybrisSuites/HYBRISCOMM6400P/hybris/bin/custom/te/tecore/classes
Can you please let me know is there any properties i missed?
This will only scan under the base directory, so all files that are not under the base directory will be ignored
sonar.projectBaseDir=D:\HybrisSuites\HYBRISCOMM6400P\hybris\bin\platform

What is the TestNG XML report pattern for Jenkins when using Maven with surefire?

I'm using TestNG Results Plugin.
I defined TestNG XML report pattern as: **\target\surefire-reports\testng-results.xml
I'm getting this error:
TestNG Reports Processing: START
Looking for TestNG results report in workspace using pattern: **\target\surefire-reports\testng-results.xml
Did not find any matching files.
What should be the correct pattern?
I just fixed this problem!
It's because that the current directory was changed to the default Jenkins workspace, something like '/Users/Shared/Jenkins/Home/workspace/your_project_name' when it ran!!
You will find it yourself if you add shell command 'pwd' before Post-build Actions in the Configuration of your project in Jenkins.
You just need configure your project in General -> Advanced... -> Use custom workspace, and fill in the full path of your project, and it will work!!!
The following pattern works for me: **/target/surefire-reports/*.xml
Also ensure that the default listeners are not disabled in your surefire configuration to ensure that surefire creates the xml reports for ReoprtNG/TestNG. Until I removed the disabling of default listeners I got the same error.

How to configure a sonar-project.properties file for code coverage?

Current my scanner is running through and only scanning the parent and skipping the rest of my nested files. If I run sonarlint (using the cli and specifying some test and source files) , it tries to analyze 37k files instead of the few I need. I have been able to skip ~3k files by adding the <sonar.skip>true</sonar.skip> property to a pom file. However, I still can't configure the project to run across certain sub-folders and print out some kind of code coverage test. (Is JaCoCo needed for the latest version(6.3,0)? Or can code-coverage be handled through some configuration?).
If Sonar seems to be analyzing too many files, it is probably because you had not set the sonar.sources=src/main/java in your sonar-project.properties file, so it defaults to the basedir and includes everything.
SonarQube can't do code-coverage itself, it just reports on coverage-reports from a tool like JaCoCo. It is funny they don't clarify these things in https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner --but with enough digging, you can find good info on that site.

Failed to publish TestNG Report in Jenkins

I have used Maven project with Selenium & TestNG to create automated scripts which I want to execute from Jenkins. In Jenkins I have added the TestNG plugin to publish the TestNG report. But it is not getting displayed. I'm getting the below mentioned error.
Error:
TestNG Reports Processing: START
Looking for TestNG results report in workspace using pattern: /target/surefire-reports/testng-results.xml
Did not find any matching files.
Finished: SUCCESS
Actual path of the TestNG report file:
E:\STUDY_MATERIAL\JAVA\WORKSPACE\SeleneniumFrameWork\target\surefire-reports\testng-results.xml
I have tried the following options but didn't work out for me.
Tried giving the full path of the TestNG report. Forward ans backward slash in the pattern.
I have tried different other options by changing the TestNG result file path but it's working for me.
Please share some inputs on how to resolve this issue.
"testng-results.xml" file is available in workspace. But it's failed to display the report. I have attached screenshot of Jenkins workspace and Console Output.
Note: I am using Jenkins 2.7.0 in Windows 10.
Jenkins Workspace
I have attached the Job configuration details of Jenkins.
Job Configuration Details
Few checkpoints :
1. Check if testng is producing this file at the specified location : /target/surefire-reports/testng-results.xml
2. TestNG needs to be configured to generate xml report
If this file is there, pass path in file locator as :
target/surefire-reports/testng-results.xml
Hopefully, you will get results on jenkins if everything mentioned above is correct.

Resources