How does Sonar Maven Plugin orchestrate tests? - maven

Is it possible to configure how the surefire plugin is called when used with mvn sonar:sonar post compile.
In my maven project I have multiple test executions, for example
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.3</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
<parallel>methods</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
<argLine>${jacoco.agent.argLine}</argLine>
</configuration>
</execution>
<execution>
<id>default-integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
<parallel>classes</parallel>
<argLine>${jacoco.agent.argLineIntegration}</argLine>
</configuration>
</execution>
</executions>
</plugin>
Note the integration tests in particular require some preprocessing ( aspectj compile for example ).
Is there any way to tell sonar to do this ? Or Indeed is it possible to generate the reports beforehand and just tell sonar to use them ?

You can have a look at the reuse report mode: http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Unit+Tests+for+Java+Project

Related

Running spock integrations tests in its own folder in maven

I am trying to only run integration tests in maven with mvn. I have my tests in src/it/java for java integration tests and src/it/groovy for Spock integration tests. When I run mvn failsafe:integration-test, only the java integration tests in src/it/java are run.
This is my pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-integration-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
<source>src/it/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<testSourceDirectory>src/it/groovy</testSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<configuration>
<testSources>
<testSource>
<directory>src/it/groovy</directory>
<includes>
<include>**/*IT.groovy</include>
</includes>
</testSource>
</testSources>
</configuration>
<executions>
<execution>
<id>groovy-compile</id>
<goals>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
Any idea what I am missing here?
It looks like the issue is that your running only failsafe:integration-test. I am able to run groovy and java tests using the command:
mvn verify
In the failsafe docs it suggests to use the verify phase here - http://maven.apache.org/surefire/maven-failsafe-plugin/index.html
If mvn verify still doesn't run your groovy tests, check your_project/target/test-classes/path_to_your_test for expected class files and make sure that lines up with the includes option in the pom.

How can I use Maven fail safe plugin to execute the pre-integration steps

I need to execute a testNG (config) test before the regression suite. This test does some config related tasks and creates users. Can somebody please explain the following POM. I am still trying to understand how to define the step in the POM.xml. The name of my test is CreateUsers.xml. Where should I define this in the following POM.XML. I am using jenkins to run these tests. Please advice. Thanks
What does id, goal and include tags mean ?
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<executions>
<execution>
<id>pre-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/PreTest.class</include>
</includes>
</configuration>
</execution>
<execution>
<id>int-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/IntTest.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I have added my per-requisite testNG as below. Please advice if this is correct ? Thanks
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<type>maven-plugin</type>
<executions>
<execution>
<id>pre-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>CreateUsers.xml</include>
</includes>
</configuration>
</execution>
</executions>

Jacoco code coverage reports not generating for integration tests, not showing in sonar

I had configured Jacoco + Sonar in Maven. I am able to generate the coverage reports for unit tests & display the coverage in Sonar as well. But, unable to generate the reports for integration tests. "jacoco-it.exec" is generated but, when I go and open the index.html, it looks empty. But, there are many integration tests got executed & passed as well. In jacoco-sessions.html, it is clearly mentioned about all the classes which are used internally. Please help me if you have any idea what is wrong in the below code wrt integration tests. I have configured Sonar also properly & given the path to pick up the results for integrations tests. Please see once and help me if you have any idea. pom.xml:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals> </execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<encoding>UTF-8</encoding>
<skipTests>false</skipTests>
<runOrder>alphabetical</runOrder>
<includes>
....<There are integration tests>
</includes>
<excludes>
....
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

maven-failsafe-plugin is not executing my integration tests

Bellow a part of my actual pom.
Testng tests for integration tests have been assigned a "integration" group in the #Test annotations.
To do little test I did not exclude the "integration" group during the test phase.
When building using for example mvn verify or mvn install the integration tests get executed in the test phase, but not the verify or integration-test phase.
The number of tests run remains 0. Somehow they are not picked up. Anyone have an idea on what might be wrong?
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.1</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.1</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<skip>false</skip>
<excludedGroups>unit</excludedGroups>
</configuration>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
<configuration>
<skip>false</skip>
<excludedGroups>unit</excludedGroups>
</configuration>
</execution>
</executions>
<configuration>
<skip>false</skip>
<excludedGroups>unit</excludedGroups>
</configuration>
</plugin>
Update: Adding TestNG as a dependency to the failsafe plugin does not
help
maven-failsafe-plugin by default includes only files matching following patterns:
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
How did you name your test class(es)?

How do I configure when cobertura tests run in maven-cobertura-plugin?

In order to fine-tune which tests are run at which times and in which environments, we have several executions set up for the maven-surefire-plugin. We set the default configuration to skip all tests, then enable them for the executions we want. This by itself works well for us.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/*IntegrationTests.java</exclude>
</excludes>
</configuration>
<execution>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*IntegrationTests.java</include>
</includes>
</configuration>
<execution>
</executions>
</plugin>
When I add the maven-cobertura-plugin to the mix, I run into problems. The cobertura goal runs, and successfully instruments my classes. However, no tests get run. I assume this is because the test execution that cobertura is running in is one that is skipped. However, I cannot find how to specify which phase and goal to set up for this execution. When I turn on all tests, the output seems to indicate that these are still running in these unit-tests and integration-tests phases/goals.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<formats>
<format>xml</format>
<format>html</format>
</formats>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
How do I need to specify a surefire execution so that the cobertura will run it against the instrumented classes?
You will note from the docs that cobertura:cobertura
Must be wired as a report
Instruments, tests and generates a report
Runs in its own lifecycle cobertura (not the default lifecycle)
Invokes lifecycle phase test before running itself
So, wiring it accordingly should automatically result in instrumentation and testing.

Resources