Maven surefire plugin output test summary xml? - maven

I am in a project that uses the Surefire and Failsafe maven plugins to run unit and integration tests respectively. The output of failsafe produces a summary of the test run in target/failsafe/failsafe-summary.xml. I would like to get a similar .xml summary report from surefire, however I can only seem to get an xml file per test suite there.
Is it possible to configure surefire to do this?

Is it possible to configure surefire to do this?
(create 1 report file/run instead of 1/test suite)
No, not without customizing surefire/surefire-report plugin.
Since the only (up-to-date) "output related" configuration options (of surefire-plugin:test) are:
encoding
redirectTestOutputToFile
reportFormat (default - "brief", alternatively - "plain" (*.txt files))
reportNameSuffix
reportsDirectory
And surefire-report plugin's only purpose is maven report (html!) generation.
For this special need, I'd recommend a custom script or (maven) plugin (basing on one of the above - they are open source). You can also try "to tackle the problem from source" by combining all of your test classes/suits into one!? :)

Related

How to include Unit and Integration test in Sonar Code Coverage

I am using below commands to build my maven code.
Compile-
-DargLine="-DDB_SERVER=localhost -DDB_PORT=5432 -DDB_NAME=sample -DDB_USER=sample -DDB_PASSWORD=sample -DDB_MAX_POOL=10" -Dcom.sample.redis=false clean compiler:compile
Unit-Test Analysis-
DargLine=-DDB_SERVER=localhost -DDB_PORT=1234 -DDB_NAME=sample -DDB_USER=sample -DDB_PASSWORD=sample -DDB_MAX_POOL=10 -Dcom.sample.redis=false -Dcobertura:cobertura-integration-test -Dcobertura.aggregate=false -Dcobertura.report.format=xml integration-test test
And using below sonar properties to capture xml to publish in sonar.
sonar.projectKey=sample
sonar.projectName=sample
sonar.projectVersion=$PipelineId
sonar.modules=admin,client-api,common,om,serviceproviders
sonar.cobertura.reportPath=target/site/cobertura/coverage.xml
sonar.sources=.
sonar.skipPackageDesign=true
sonar.sourceEncoding=UTF-8
Being Multi module the code coverage is showing only 9.4%. Am I missing anything. I don't see any error logs as well.How can I achieve the same using coverage tool like Jacoco.
SonarQube - Version 5.1.1 - LGPL v3
Maven has a lifecycle Maven Lifecycle where each of the targets includes the ones before it. e.g. "test" includes "compile", "integration-test" includes "test", etc. You generally need to only include the target at the tip of the lifecycle. e.g. "mvn test" means (compile AND run the tests).
I'm thinking you want to run the "mvn verify" goal, which is compile, run tests and integration tests, and then run verifications (coverage checks, etc). Cobertura has a plugin that should integrate with maven and tap into various goals to run its pieces at appropriate times. I'm guessing you are messing up cobertura by having multiple targets and trying to break it into pieces - i.e. overwriting the instrumentation or something.
Similarly, you might find using jacoco easier than cobertura. It hides the instrumentation, and integrates pretty seemlessly with maven.
Good luck.

how to clean jacoco.exec after running integration tests in a multi-module project

I have a multimodule Maven project, it is analyzed using the Sonar Maven runner and coverage is done with Jacoco. For our integration tests we want to see the coverage across all modules (because they are integration tests after all).
Therefore we configure the jacoco-it.exec file to be in ${user.dir}, with the appendproperty to true. This way all modules append their information to the same location and coverage is calculated over all modules.
But since append is true the file will still be there on a next run, since it isn't placed in a directory that maven cleans. This leads to incorrect coverage reports.
What is the best way to clean up this file after a sonar run? Ideally I would like to configure this in the same pom profile as our jacoco/sonar configuration, so that no other projects need to remember to set a clean step in Jenkins or whereever. The sonar/jacoco configuration is in a company wide parent pom file.
Since you are using Maven, you could try and use the maven-antrun-plugin to delete the file.
I don't know how you run Sonar Maven, but you can either bind the maven-antrun-plugin task to a phase after the one the Sonar Maven Runner is bound to (and you would have the file deleted automatically and the end of each run) or you can call the maven-antrun-plugin from the command line.

Feed sonarqube jacoco widget with csv/html reports (instead of *.exec)

I got a root maven project, under which there are many independent modules (e.g. module_A, module_B, etc.).
One of these module is my integration-tests module, and it uses all the above external modules.
In order to have code coverage for all modules used by integration-tests, I use a workaround based on maven-ant (see this blog post).
Problem is:
The above generates csv/html report, yet sonarqube jacoco widget parses only jacoco*.exec files - which results in 0% code coverage.
Question is:
EDIT
here's an example project for the problem above.
You don't need to use that workaround. You can provide Sonar with integration tests coverage file with following property (you shall use it while executing sonar:sonar goal):
-Dsonar.jacoco.itReportPath=<coverage_file>
Here is detailed documentation:
http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Integration+Tests+for+Java+Project
To sum up:
Compile all your modules.
Execute integration tests with jacoco enabled.
Execute Maven Sonar build adding mentioned property in command line.
I have prepared example project generating both unit and integration coverage results, you can check it here:
https://github.com/octosan/unit-and-integration-jacoco-coverage-with-sonar
You have to:
download newest Sonarqube version and start it
execute command:
mvn clean install sonar:sonar -Dsonar.jacoco.itReportPath=<absolute_path>/itest/target/jacoco-it.exec
add integration coverage widged in project dashboard in Sonar

How to get a nicely formatted JUnit report out of a Maven test run?

I have:
a pom.xml which includes junit 4.4, log4j 1.2, and maven-surefire-plugin
Some Scala tests with #RunWith(classOf[JUnitRunner]) and FunSpec
To run it, I do maven clean test etc.
I can generate Surefire HTML reports with a surefire-report Maven switch, but they're not very detailed and I don't think they conform to standard JUnit reports?
Any hints on how I can get a JUnit report output with a nice format (e.g. HTML + CSS)?
Thanks.
The surefire-report:report and surefire-report:report-only goals will generate HTML report for Maven test execution. As part of Maven site generation it could be further styled using one of the skins
You may try Allure Reports which can generate a report website .
A sample project here

Jenkins JDepend Plugin reports JAR classes

I am using 1.467 and Jenkins JDepend plugin 1.2.3.
When I build, it generates the JDepend report.
No problem.
THe problem is that I get reports on classes like:
org.apache.commons.fileupload
org.hibernate.ejb
Packages report on classes that I didn't write.
How I configure this so that it JDepend only run on classes that I write?
com.mycompany.myproject
for example.
Thanks.
JDepend Maven Plugin looks quite old and seems to be in beta. It does not support a way to exclude packages/classes. I assume jenkins JDepend Plugin uses the information from this to generate the report.
JDepend itself provides options to include/exclude classes/packages from analysis. JDepend ant task provides support for this as well.
One possibility would be to use jdepend ant task using maven antrun plugin (or directly invoke jdepend using maven exec plugin) to do the analysis and specify the report file to jenkins plugin for reporting.
i got it to work by setting the goal to "jdepend:generate" and setting the Pre-generated JDepend file to "target/jdepend-report.xml"

Resources