maven failsafe plugin - maven

I am trying to run only selenium tests using mvn failsafe plugin. I created a separate profile to run only the selenium tests but mvn is not able to find them. my project structure looks like
moduleA
scr/main/...
src/test/integration/java/...
scr/test/unit/java/...
moduleB
scr/main/...
src/test/integration/java/...
scr/test/unit/java/...
moduleC (only for selenium tests)
scr/main/java/com/selenium/A.java
src/test/java/...
Since I have new directories for the unit and integration tests in moduleA and B. I have defined the following in pom.xml (listingB) to let Maven know about the additional test directories. So far so good but when I add selenium tests in moduleC and I want to run only selenium its not running selenium tests. I created a new profile (listingA) to run selenium tests. Any help is appreciated.
listingA:
<profiles>
<profile>
<id>selenium</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>verify</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/selenium/*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
listinB:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/test/unit/java</source>
<source>${basedir}/src/test/integration/java</source>
<source>${basedir}/src/test/common/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${basedir}/src/test/integration/resources</directory>
<directory>${basedir}/src/test/unit/resources</directory>
<directory>${basedir}/src/test/common/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

The first thing which came into my mind is why you are separating the unit- and integration tests by using a different folder, cause in Maven the separation between unit- and integration tests is done by naming convention.
The unit tests have to be named like
**/Test*.java
**/*Test.java
**/*TestCase.java
The integration test have to be named like:
**/IT*.java
**/*IT.java
**/*ITCase.java
This means in other words you can put your integration tests and your unit tests into the same folder which is src/test/java without any problem. The execution of the integration tests is not influenced by this.
If you would call
mvn clean package
only the unit tests will be run. If you need to run the integration tests as well you can simply use:
mvn verify
If you wan't to skip the unit tests you can use the following:
mvn -DskipTests=true verify
For the separate selenium module which you have created the best solutin is to put the integration tests into the usual folder src/test/java with the appropriate naming convention.
The problem you might have is if your integration tests needed some special resources you might move those integration tests into the separate module with integration tests only.

If your Selenium tests are in the src/test/java folder, delete the <includes> parameter from the configuration of maven-failsafe-plugin in listingA
Name your Selenium test classes like:
**/IT*.java
**/*IT.java
**/*ITCase.java
(as khmarbaise has suggested)
Failsafe runs integration tests using Surefire, therefore don't name your Selenium test classes like:
**/Test*.java
**/*Test.java
**/*TestCase.java
because this will make them run in test fase and not in integration-test fase as you plan.
I have a configuration similar to yours and it works.

Related

How to run before and after maven test

mvn test
I am looking to initialize certain resources before all test cases executed using maven test and also to destroy them after all test cases are executed.
I have looked into jUnit #BeforeClass, #AfterClass, #Before and #After, but none of them are helpful.
I have tried to use maven life-cycle phases i.e. pre-integration-test as below, but even in this case the expected Test case (TestPostgresqlEmbedded) doesn't get executed first.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<executions>
<execution>
<id>test-init</id>
<configuration>
<runOrder>alphabetical</runOrder>
<includes>
<include>**/TestPostgresqlEmbedded.java</include>
</includes>
</configuration>
<phase>pre-integration-test</phase>
</execution>
<execution>
<id>test-all</id>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
</execution>
</executions>
</plugin>
How can I achieve this ?
Use the integration-test phase for tests with databases. Then you have pre-integration-test to set up your database resources, and post-integration-test to destroy them.

is there any way to skip cucumber scenarios while running unit steps?

mvn install is running cucumber steps also. in our local development, we need to run only unit tests and not the cucumber scenarios. Tried with -Dtest=!com.mycompany.* no luck.
And at the same time, we need to skip our Unit Tests while executing a cucumber scenario, is that possible?
This could be achieved by using build profiles. You should run your unit tests by using the surefire plugin and the cucumber scenarios with the failsafe plugin. Naming conventions for automatic running of tests for surefire and failsafe.
<profiles>
<profile>
<id>jenkins</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Run this with mvn clean install -Pjenkins. This will only run the integration tests ie scenarios.
To run the unit tests just use mvn clean install. Surefire is invoked ie unit test, by default but not failsafe.

Maven war packaging skipped prior to tests being run

I have a maven project which has some Selenium based integration tests to run in a package called sit. I have configured the failsafe plugin to include test files in the package eg. <include>sit/**/*Test.java</include>.
I am trying to run mvn clean install and when it launches the tests I noticed it hasn't actually run the maven-war-plugin i.e. not packaged it into a war prior to running the tests. If however I run mvn clean install -Dmaven.test.skip=true then it does successfully package the war. (I also have the cargo plugin configured to deploy the war which is how I noticed there was an issue as it fails to deploy a non existing war)
The structure of my pom is similar to the below
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>a</groupId>
<artifactId>a</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>sit/**/*Test.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Any ideas why it doesn't run the war plugin without setting -Dmaven.test.skip=true?
It does in both scenarios create the class files
Try to rename your test classes for integration tests to be with an *IT.java suffix (or prefix...), since this is the convention for tests to be run by maven-failsafe-plugin.
The *Test.java is another convention for running the tests by maven-surefire-plugin.
So no need to exclude them, just rename it.
Ok it seems the issue was maven-surefire was trying to run the integration tests as unit tests, so we have to explicitly exclude the integration tests from surefires unit tests
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
...
<configuration>
<excludes>
<exclude>sit/**/*Test.java</exclude>
</excludes>
</configuration>
</plugin>

mvn integration-test command pulls in unwanted unit tests for execution

When I run mvn test it executes unit tests only but when I run mvn integration-test it executes both unit test and integration test even after configuring the maven-failsafe-plugin and excluding the *Test.java file. Not sure what I am missing here. Also worth mentioning that I have not put in maven-surefire-plugin in my pom.xml. Not sure if that is creating this problem. Please guide.
pom.xml
<!-- Integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18</version>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Unit Test Class File:
com.study.jenkins.ut.MyUnitTest.java
Integration Test Class File:
com.study.jenkins.it.PageIT.java
The maven-surefire-plugin is part of the lifecycle, which is always bound to the test-phase for Java projects. Calling integration-test means that all lifecycle-phases up to the integration-test phase are executed. So the MyUnitTest will always be executed (which is a good thing).
Your includes/excludes have no effect, these are already the defaults for the maven-failsafe-plugin, see http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#includes

How to integrate cucumber-jvm with maven build

i'm trying to put some bdd into my eclipse plugin project, but can't figure out how to run my integration tests during maven build fase. To write my tests i'm using SWTBot framework.
I already did the feature generation fase, and setup my tests. How do i setup my pom to run my integration tests?
I use below configuration and run mvn clean verify. If you don't want to run tests in parallel, remove parallel, perCoreThreadCount and threadCountClasses tags.
Make sure to update the regular expression to match your test naming convention <include>**/Run*.java</include>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>acceptance-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<outputEncoding>UTF-8</outputEncoding>
<parallel>classes</parallel>
<perCoreThreadCount>true</perCoreThreadCount>
<threadCountClasses>10</threadCountClasses>
<argLine>-Xmx1024m</argLine>
<argLine>-XX:MaxPermSize=256m</argLine>
<includes>
<include>**/Run*.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

Resources