How to run test sequentially with Maven surefire - maven

After searching everywhere on internet I post my question here for an possible answer. First of all, sorry for my bad English, I'm a French speaker.
I'm trying to run Java tests in my Jenkins for a continuous integration project. But Maven run the tests in parallel. The First 10 tests need to be runned sequentially because they initialize database and some templates.
I found this link on the official page : https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html
but when I use #NotThreadSafe on my Java test class as they said I got an error :
error: cannot find symbol
could not parse error message: symbol: class NotThreadSafe
I use Maven 2.2.1, Junit 4.6
Do you know how could I do please ?
Edit : my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/P99ReleaseTestSuite.java</include>
</includes>
<!--
<excludes> <exclude>**/*Test.java</exclude> </excludes>
-->
<skipExec>${surefire.tests.execution.skip}</skipExec>
<testFailureIgnore>${surefire.tests.execution.ignore-error}</testFailureIgnore>
<forkMode>once</forkMode>
<argLine>-Xms512m -Xmx2048m</argLine>
</configuration>
</plugin>
I run these tests in a linux server. On windows, in my eclipse, all tests run sequentially

Related

Tests run slower with Maven than with IDE

I'm facing some test time execution issue related to local test execution with Maven. I have around 250 tests which run about 15 min on the IDE test runner and about an hour (55min to be exact) when executing them with maven locally. I tried a lot of configurations to make test execution in parallel but neither of them work for me, the time is still the same...probably I'm doing something wrong. Can anyone help on this? The last maven surefire plugin configuration that I tried is the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
And for test execution command I've used:
mvn test
mvn surefire:test

why does my maven tests fails when run as a whole and passes when ran individually?

Hi I am having a strange scenario.
I have a spring boot java project . I have lots of Junit tests as well.
when i run the tests from the terminal by issuing the command mvn clean test, 2 tests belong to a class fails
however if run the those tests belonging to that class from the eclipse Run as >> Junit tests it passes .
any idea why does this happens ? and how can i fix it ?
the following is my sure fire plugin configuration
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<!-- Force alphabetical order to have a reproducible build -->
<runOrder>alphabetical</runOrder>
</configuration>
</plugin>
thank you

Difference between v2.18 and v2.22.0 of Maven surefire plugin

I configured surefire v2.18 to exclude tests in packages like e2e.com.mycompany.myapp but not like com.mycompany.myapp.e2e and it was working but if I use v2.22.0 I get a different behavior: both are excluded. Is it a bug?
My configuration is:
<configuration>
<excludes>
<exclude>e2e/**</exclude>
</excludes>
</configuration>
I also tried with:
<configuration>
<excludes>
<exclude>e2e/**/*Test*.java</exclude>
</excludes>
</configuration>
How can I get the same behavior with the new version? I'm using Maven 3.3.9.
You could go through following link which explains enhancements made with v2.19 onwards for exclusion and inclusion of tests:
https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
Following is brief snapshot captured from above link for inclusion and exclusion of packages and classes:
Following link explains release notes for v2.22:
https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12317927&version=12343425

How to redirect unit test output Maven

I'm working in a Java 8 project built using maven. Whenever I do a mvn install the root of my project gets polluted with the output files produced from my unit tests.
How can I redirect that output to somewhere else (maybe target directory) rather than the root directory of my project?
I thought about rewriting the unit tests to point the output to target but that seems a bit silly to me. Perhaps there is a plugin or a maven directive which might do what I want to accomplish?
I tried configuring the surefire plugin but this didn't help :(
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- Set working directory for content -->
<workingDirectory>target/test-classes</workingDirectory>
<useFile>false</useFile>
<!-- Just set to some large numbers for all tests to work -->
<argLine>-Xmx1g -Xss1m -XX:MaxPermSize=128m</argLine>
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
Looks like my initial attempt was almost there, just upgraded the version and it worked :)
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<!-- Set working directory for content -->
<workingDirectory>target/test-classes</workingDirectory>
<useFile>false</useFile>
<!-- Just set to some large numbers for all tests to work -->
<argLine>-Xmx1g -Xss1m -XX:MaxPermSize=128m</argLine>
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
There is new configuration option redirectTestOutputToFile since version 2.3 for exactly this purpose.

How to properly configure the Selenium Maven Plugin to work with Xvfb to run headless

Background:
I'm using selenium-server-2.25.0 in conjunction with J-Unit 4 to run through a handful of UI testing scenarios for my GWT app. In my IDE (Netbeans 7.2), I can right-click on my project, choose "Test", and see Firefox windows pop up all over (as they should) with the Selenium tests running as expected. From the command line, I can also run mvn integration-test and see the same.
Goal:
I'm trying to get these tests to run headless in an Xvfb display, but I seem to be having trouble getting this to work with Maven. I can manually run export display=:2 (:2 being my Xvfb display) beforehand, and then the tests then DO run in the invisible display successfully.
Issue:
Nothing seems to change at all when I include the full <plugin> entry from here in my pom.xml and run mvn integration-test. I still see Windows popping up all over and the tests running not in the Xvfb display. If I take it out and run again, same results. When I change the phase from pre-integration-test to qwertyasdf however, Maven does complain about an invalid lifecycle phase - so I know it's not completely ignoring it, and I am editing the appropriate pom.xml.
Thanks!
It turns out the 'start-server' and 'stop-server' goals are for starting/stopping SeleniumRC servers. This is NOT what I wanted, as all my tests use the WebDriver API instead.
Apparently the 'xvfb' goal in the pom DOES start an Xvfb session during the specified lifecycle phase - I guess I just didn't see it before. And in it's configuration, you specify where to write a props file detailing which display Xvfb is running on. In the Java code, this file can be read and the value passed to the FirefoxBinary used when creating the WebDriver.
The relevant pom.xml bits are as follows:
<properties>
<displayProps>target/selenium/display.properties</displayProps>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<display.props>${displayProps}</display.props>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>xvfb</id>
<phase>test-compile</phase>
<goals>
<goal>xvfb</goal>
</goals>
<configuration>
<displayPropertiesFile>${displayProps}</displayPropertiesFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This starts Xvfb on the first free display (:20 or above) and writes the value to the props file that I read and use later in my Java code.
String xvfbPropsFile = System.getProperty("display.props");
FirefoxBinary ffox = new FirefoxBinary();
ffox.setEnvironmentProperty("DISPLAY", /*read value from xvfbPropsFile*/);
WebDriver driver = new FirefoxDriver(ffox);
Now the driver will be in control of the Firefox instance spun up in the appropriate display. Voila!

Resources