I have been searching for this everywhere and whilst there are documentations and answers all over the place, I can't seem to be able to make this work
I am currently working on a multi-modules project with thousands of tests and because these take hours to run, I thought it will be a good idea to run them in parallel(just the tests not the modules)
To achieve this, I found out that jUnit 4.7 or later with maven-surefire-plugin can be used to run tests in parallel
Here's my configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>classes</parallel>
<threadCount>10</threadCount>
<argLine>-Xverify:none
-enableassertions -Djava.util.Arrays.useLegacyMergeSort=true #{argLine}</argLine>
<useSystemClassLoader>false</useSystemClassLoader>
<includes>
<include>**/*Test.java</include>
<include>**/Test*.java</include>
</includes>
<excludes>
...
...
</plugin>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/Test*.java</include>
</includes>
<!-- Required until JDK >=1.7.0u72, see https://code.google.com/p/powermock/issues/detail?id=504 -->
<runOrder>alphabetical</runOrder>
<argLine>#{argLine}</argLine>
</configuration>
</plugin>
Running on Apache Maven 3.3.9
There are all kind of tests, (RunWith, PowerMock,BeforeClass,Before etc)
Here's what I'm doing to test if my configuration is working or not
First I build all the modules
mvn clean install -Pbuild-all -DskipTests
Next I run the specific module tests using the following command line
mvn test -DfailIfNoTests=false -pl module1
The above doesn't seem to be running in parallel as I get stuck in some tests and it doesn't go any further
Furthermore, I tried to run multiple jUnit test classes rather than everything in the module but I'm not sure whether they are running in parallel, I used the following command
mvn test -DfailIfNoTests=false -Dtest=testClass1,testClass2,testClass3 -pl module1
The above finishes in 20 seconds but I'm not sure whether it has run in parallel
Is there anything I'm missing?
Related
I've got a pom.xml that looks roughly like this:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<configuration>
<includes>
<include>my/package/path/**/*</include>
</includes>
</configuration>
<!-- more stuff -->
</plugin>
When I run my unit tests then call mvn jacoco:report locally on my laptop, everything is fine, and only the stuff in my.package.path is included. My coverage is 86%. However, when I let Jenkins run the unit tests, using the same commands, then call jacoco(execPattern:'target/jacoco.exec') in my Jenkinsfile, I end up with all code included in the report attached to the build, so my coverage ends up being 2% because I didn't write tests for a bunch of 3rd party libraries.
How do I fix this?
You may exclude the 3rd party packages:
<excludes><!-- Exclude class from test coverage -->
<exclude>**/*com/3rdparty/path/*</exclude>
</excludes>
I was able to solve it by using the inclusionPattern parameter in the call to jacoco:
jacoco(execPattern: 'target/jacoco.exec',inclusionPattern: 'my/package/path/**/*')
I am using JUnit's categories to split my tests into different categories and using maven to compile and run my test (surefire and failsafe).
Question is, how to I choose which category of tests are executed from command line?
something like: mvn clean install -DloadTests.
my failsafe plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.plugin.version}</version>
<configuration>
<!--Exclude load tests by default-->
<excludedGroups>com.test.lib.categories.LoadTestCategory</excludedGroups>
</configuration>
</plugin>
You could do that with profiles and specify the plugin configuration in there.
<profiles>
<profile>
<id>noLoadTests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.plugin.version}</version>
<configuration>
<!--Exclude load tests by default-->
<excludedGroups>com.test.lib.categories.LoadTestCategory</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
And then run maven
mvn test -PnoLoadTests
If you only ever need to exclude/include one specific category you could also define a property in the profile and use that in the . For more info you can look here
Edit: The other provided answer is the better one in this case, but profiles allow for various advanced configurations.
According to the documentation, user property is called groups. Therefore this should work:
mvn clean install -Dgroups=com.test.lib.categories.LoadTestCategory
I am developing a webapp running on Tomcat7, and using maven for dependencies/automated builds. Yesterday I started using the sass-maven-plugin, which is great. Its goal sass:update-stylesheets processes sass files and outputs css. Unfortunately, I can't have it executed during the webapp packaging. I am pretty new to maven too, so I might have missed something. Here's my understanding :
when I type mvn tomcat7:deploy, maven executes the deploy goal defined in the tomcat7 plugin
this plugin goes through some phases of the development lifecycle. More specifically, as mentioned in the doc, it "invokes the execution of the lifecycle phase package prior to executing itself."
if I map the goal sass:update-stylesheets to the package phase in <build><executions/></build>, it should be executed everytime I deploy/redeploy my app.
When I run mvn sass:update-stylesheets independently of tomcat7:deploy, everything is smooth. sass-maven-plugin gets the .scss files from src/main/resources, processes them and places the output in src/main/webapp/resources, where I want it to be to be deployed with my webapp. Unfortunately, if I don't run the command prior to tomcat7:deploy, I don't get any css for my pages. What did I get wrong? Also, is there any way I could map the sass:update-stylesheets to the phase process-resources, for instance, which would make more sense? Lastly, if this all works, will Eclipse's incremental build pick it up?
Here's my pom.xml (the relevant parts)
...
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
...
</configuration>
</plugin>
<!-- SASS processing -->
<plugin>
<groupId>org.jasig.maven</groupId>
<artifactId>sass-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>generate-css</id>
<phase>package</phase>
<goals>
<goal>update-stylesheets</goal>
</goals>
</execution>
</executions>
<configuration>
<useCompass>true</useCompass>
<resources>
<resource>
<source>
<directory>${basedir}/src/main/resources</directory>
</source>
<destination>${basedir}/src/main/webapp/resources</destination>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Thanks in advance for your help.
You have configured the stuff in the plugin management section. Please move the execution and it's binding to the build section (that is, out of the pluginManagement's plugins section into build's plugins section).
this might seem like a straightforward question and there are a lot of answers but none of them actually work. People always suggest to use -Dmaven.test.skip=true or -DskipTests but these options completely skip the tests and they are never moved to the local repo, so when I install other modules which depend on the existence of the tests in local repo they fail.
I need the tests to go under the the same group id and artifact id as the main classes only with different classifier and scope so I could reference them in other project like this
<dependency>
<groupId>myGroupId</groupId>
<artifactId>myArtificatId</artifactId>
<version>4.0-SNAPSHOT</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
I guess you want this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
This will package the -test classified jar. Be aware that if you use -DskipTests, surefire and failsafe will skip the executions of the tests, while if you use -Dmaven.test.skip=true tests will completely be ignored by surefire, failsafe and compiler.
We have our POM defining the maven-surefire-plugin as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<reuseForks>false</reuseForks>
<forkCount>1</forkCount>
<argLine>-Xms64m -Xmx256m</argLine>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
However, our Java tests (which involve some parallel tests and static singletons) only run properly when we run our test phase/build using:
mvn test -DforkMode=always
Strangely, even if we change our <configuration> to use (instead of the newer options):
<forkMode>always</forkMode>
And run:
mvn test
It will fail. But then if we run:
mvn test -DforkMode=always
It will pass. Instead of the newer options, it still will only work if we explicitly provide forkMode on the command line. We have tried this with multiple versions of the surefire plugin, to the same effect.
Are there any locations where this property could be overridden, or known issues in which the XML configuration is not properly used?
Rookie mistake. The configuration I was using was listed in a separate <profile> block that was not being executed. The profile with:
<activeByDefault>true</activeByDefault>
Did not include its own Surefire configuration at all (so it didn't show up in a search), and was using inherited values, which explains why the command-line system properties were able to affect its behavior.