Run Junit Suite using Maven Command - maven

I have multiple Junit test suites (SlowTestSuite, FastTestSuite etc). I would like to run only specific suite using maven command. e.g.
mvn clean install test -Dtest=FastTestSuite -DfailIfNoTests=false
but its not working. Just not running any test at all. Any suggestions please.

I have achieved this by adding property into pom as:
<properties>
<runSuite>**/FastTestSuite.class</runSuite>
</properties>
and maven-surefire-plugin should be:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>${runSuite}</include>
</includes>
</configuration>
</plugin>
so it means by default it will run FastTestSuite but you can run other test e.g. SlowTestSuite using maven command as:
mvn install -DrunSuite=**/SlowTestSuite.class -DfailIfNoTests=false

The keyword you missed is maven-surefire-plugin :http://maven.apache.org/plugins/maven-surefire-plugin/.
Usage is :
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.1</version>
<configuration>
<includes>
<include>**/com.your.packaged.Sample.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
If you make a little search on stack overflow, you may find information :
Running a JUnit4 Test Suite in Maven using maven-failsafe-plugin
Using JUnit Categories with Maven Failsafe plugin
In addition, you may define profile, like fastTest, that will be triggered by adding parameter to cmd line :
mvn package -PfastTests
This profile would include some inclusions too.

Related

how to run integration tests with maven build from eclipse or from command line

I created a integration test class "sampleIT.java",to test this class, I added run configurations for junit in eclipse under vm arguments like
-Dspring.profiles.active=it-dev
-Dspring.config.name=it-client
the test is running successfully.
I was trying to run maven build and it should also cover(run)integration test class. I added run configuration for maven build, added goals as
clean verify -Dspring.profiles.active=it-dev -Dspring.config.name=it-client
I added these contents in pom.xml
<profiles>
<profile>
<id>it</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/*IT</include>
</includes>
<argLine>-Dspring.config.name=it-dev</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I tried in command line as well, but the integration test is not running
how to run maven build to run all junits and integartion tests in eclipse or from command line?

If argument in maven Surefire plugin, skip E2E tests

I couldn't seem to find anything on this but I'm curious if I can pass an argument during runtime to skip all of our projects E2E tests.
Is there anyway for me to do something like the segregated exclude block in the following pom example?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude unless="${skip.E2E.tests}> **/*E2E*.java</exclude>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
Then I could just call mvn clean install -Dskip.E2E.tests=true. Anybody seen anything like this?
I suppose I could do something like...
<exclude>${name.of.tests.to.exclude}</exclude>
and then mvn clean install -Dname.of.tests.to.exclude=**/*E2E*.javabut I would prefer to get an easy true or false argument to set rather than this in case some of the tests I want to skip do not include E2E and I need to add them to a list.
It's hard to tell just from the snippet of your pom that you are showing, but it looks like you are using surefire for both your unit and your e2e tests. Instead, you should consider using the failsafe plugin for e2e.
One benefit is that the e2e tests will run in a different stage so you get the behavior looking for by default. They are run during the verify stage of the project build. So, you can run mvn test to run unit tests only.
You can configure your project to use fail-safe like this:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Run them using: mvn verify
Running mvn install -DskipITs will skip only integration tests, while still running unit tests.
And running mvn install -DskipTests will skip both integration and unit tests.
If you want to implement such a condition, you could use Maven profiles and have two configuration:
The default one as part of the normal build, not skipping the E2E tests
the profiled one skipping them
The profile could be then activated upon property or direct activation.
As an example you could have:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>skip.E2E.tests</id>
<activation>
<property>
<name>skip.E2E.tests</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude>**/*E2E*.java</exclude>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Note: the default Maven Surefire Plugin applying to normal build and then a profiled one.
Running:
mvn clean install
Will not activate the profile and your build will skip the tests. While running:
mvn clean install -Pskip.E2E.tests
or
mvn clean install -Dskip.E2E.tests=true
Will activate the profile and as such add the exclusion to the tests execution.
So this is exactly the scenario you were looking for, I presume.
Alternatively and as suggested by #AndrewEisenberg in the another answer, you could use the Maven Failsafe Plugin for different type of tests. The main two differences are that: it has different phase bindings AND when it fails, it does it in a safer way. As from official documentation:
If you use the Surefire Plugin for running tests, then when you have a test failure, the build will stop at the integration-test phase and your integration test environment will not have been torn down correctly.
The Failsafe Plugin is used during the integration-test and verify phases of the build lifecycle to execute the integration tests of an application. The Failsafe Plugin will not fail the build during the integration-test phase, thus enabling the post-integration-test phase to execute

How can I run Cucumber test using explicit Maven goal?

I have an application contains both Cucumber and JBehave test, I want to be able to run one of them optionally every time, I can do that with JBehave by explicit Maven goal, but the problem is that Cucumber run implicitly with each build or test, is there anyway to stop and run it o choice?
You can achieve this by configuring the Maven Surefire Plugin as part of your default build or/and via a profile.
If your Maven build section, you can skip the Cucumber tests by default (given that they either have all the same suffix or belong all to the same package, alternatively you can arrange them to meet any of these two criterias):
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<!-- classes that include the name CucumberTest, as an example -->
<exclude>**/*CucumberTest*.java</exclude>
<!-- classes in a package whose last segment is named cucumber, as an example -->
<exclude>**/cucumber/*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
As such, Maven by default (as part of the default build) will skip your Cucumber tests.
Then, you can configure a Maven Profile to run exclusively the Cucumber tests with a counterpart of the Maven Surefire Plugin configuration as following:
<project>
[...]
<profiles>
<profile>
<id>cucumber-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<!-- Include your Cucumber tests, as an example -->
<include>**/*CucumberTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<profile>
</profiles>
[...]
</project>
Then running mvn clean install -Pcucumber-tests will run your Cucumber tests.
This approach would give you more flexibility on configuration in both scenarios (default or cucumber tests build) and you could swap the behavior according to your needs.
Alternatively, for a simpler (but less flexible) approach, you could follow the suggestion on another SO answer and use a Maven property to have a switch cucumber tests on/off as following:
<properties>
<exclude.cucumber.tests>nothing-to-exclude</exclude.cucumber.tests>
</properties>
<profiles>
<profile>
<id>exclude-cucumber</id>
<properties>
<exclude.cucumber.tests>**/*Cucumber*.java</exclude.cucumber.tests>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>${exclude.cucumber.tests}</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Using the configuration above, by default Maven will execute Cucumber tests and skip them when executing mvn clean install -Pexclude-cucumber (the profile will change the content of the exclude.cucumber.tests property and as such change the Surefire plugin filter). You can of course swap the behavior is as well and have an include-cucumber profile instead.

TestNG - Run single test by passing the test name though command line

Currently we are using maven profiling to run testng test suites. Below is my profile
<profile>
<id>BAT_All</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>src/main/resources/testplans/BAT_All.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
In this case we are using below command to run all the test cases in BAT_All.xml file and working as expected
mvn clean install -PBAT_All
Now I need to run single test case using test case name. I tried with below command but its not working.
mvn clean install -Dtest=verify_Home_Page_Title
Is it possible to run single test case with maven profiling
mvn clean test -Dtest=classname#testname

How can I skip tests in maven install goal, while running them in maven test goal?

I have a multi-module maven project with both integration and unit tests in the same folder (src/test/java). Integration tests are marked with #Category(IntegrationTest.class). I want to end up with the following setup:
If I run mvn install, I want all tests to compile, but I do not want to execute any.
If I run mvn test, I want all tests to compile, but execute only unit tests.
If I run mvn integration-test, I want to compile and execute all tests.
The important point is, I want this configured in the pom.xml without any extra commandline arguments.
Currently I came up with the following setup in my parent pom.xml, where the only problem is #1, where all tests are executed:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${project.java.version}</source>
<target>${project.java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<excludedGroups>cz.cuni.xrg.intlib.commons.IntegrationTest</excludedGroups>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>
<configuration>
<groups>cz.cuni.xrg.intlib.commons.IntegrationTest</groups>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
All child modules have the following plugin configuration in their pom.xml, which I believe should inherit from the parent pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
I tried using <skipTests>true</skipTests>, but it disables test execution for all goals, which is not what I want (violates #2 and #3). It is also quite weird, that mvn test honors the skipTests=true option...why would I want to run it in the first place??
After hours of googling and trying different combinations, I am hesitant whether it is even possible to not run tests in mvn install, while at the same time run them in mvn test. I hope someone proves this wrong. ;)
I am also willing to accept a solution, where mvn install would execute only unit tests, but I don't think it makes much difference.
It sounds like you didn't understand the concept of the build life-cycle in Maven. If you run mvn install all life-cycle phases (including the install phase itself) run before the install phase. This means running the following phases:
validate
initialize
generate-sources
process-sources
generate-resources
process-resources
compile
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources
test-compile
process-test-classes
test
prepare-package
package
pre-integration-test
integration-test
post-integration-test
verify
install
which means in other words the test as well as integration-test life-cycle phases are included. So without any supplemental information it's not possible to change the behaviour as you wish it.
It could be achieved by using a profile in Maven:
<project>
[...]
<profiles>
<profile>
<id>no-unit-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
[...]
</project>
So your first requirement:
If I run mvn install, I want all tests to compile, but I do not want to execute any.
can be achieved by using the following:
mvn -Pno-unit-test test
If I run mvn test, I want all tests to compile, but execute only unit tests.
This can simply achieved by using the plain call:
mvn test
cause the integration tests phase is not run (see the build life cycle).
If I run mvn integration-test, I want to compile and execute all tests.
This means running the default which includes running the test phase which will run the unit tests (maven-surefire-plugin) and furthermore running the integration test which are handled by the maven-failsafe-plugin. But you should be aware that if you like to call the integration tests you should using the following command:
mvn verify
instead, cause you missed the post-integration-test phase in your previous call.
Apart from the above you should follow the naming conventions for unit and integration tests where unit tests should be named like the following:
<includes>
<include>**/*Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
and integration tests should be named like the following:
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
I hope you have configured the maven-failsafe-plugin like the following which is needed to bound the maven-failsafe-plugin to the correct life-cycle-phases:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
as you correctly did, but you should be aware that the include tags work on the source code (.java) and not on the compiled names (.class). I wouldn't use the Category annotation, just simply using the naming conventions makes the pom simpler and shorter.
According to the Failsafe Plugin documentation
mvn install -DskipITs
is what you want.
What OP stated in his question:
If I run mvn install, I want all tests to compile, but I do not want
to execute any.
If I run mvn test, I want all tests to compile, but execute only unit tests.
If I run mvn integration-test, I want to compile and execute all tests.
is perfectly valid and extremely easy to achieve.
EDIT: except first condition, which acts againts the maven nature. The best way here would be simply do mvn install -DskipTests
All you need is following snippet in pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
and to stick to the maven naming conventions for unit and integration tests (as #khmarbaise already stated). So generally name you integration tests with IT suffix (for example MyIntegrationTestIT.java) and let maven-failsafe do its job.
In that way, you do not even need JUnit categories (although sometimes they can be quite useful).
That's it :)
mvn test executes only unit tests
mvn integration-test executes all tests
mvn failsafe:integration-test runs only integration tests
mvn clean verify when you want to be sure, that whole project just works
Some personal advices
Keeping integration tests separately from unit tests lets you easily run within your IDE all tests in some package. Usually additional directory called test-integration (or integrationtest) is used for this purpose.
This is also easy to achieve with maven:
<plugin>
<!-- adding second test source directory (just for integration tests) -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-integration-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test-integration/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
And then move your integration tests to that directory. It should look like:
src
main
test
test-integration
Integration tests usually needs more memory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
...
<configuration>
<argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>
</configuration>
</plugin>
This post explains how to skip integration tests, no matter what plugin you are using for these tests.
Basically, what you do is define a profile and put all your integration-tests related xml code inside that profile. Than you activate it when a property -DskipIntegrationTests is missing.
You can do the same for unit tests: write a profile and activate it when -DskipUnitTests is missing.
Then, you could do:
mvn install -DskipIntegrationTests -DskipUnitTests # (runs install without any tests)
mvn test # (runs unit tests)
mvn post-integration-test # (runs all tests)
The maven-failsafe-plugin docs has a section titled "Skipping by Default."
Sadly, the steps that page describes don't work as written. However, a slight change to those steps will make it work:
In the properties section of pom.xml, add this:
<skipITs>true</skipITs>
Then add the skipTests property to the plugin section of maven-failsafe-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skipTests>${skipITs}</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
So now, an mvn install by default will execute unit tests, but not integration tests.
But an mvn install -DskipITs=false will execute both unit tests and integration tests.
Footnote: Bad documentation played a big part on why Maven was so disliked for such a long time.
mvn test-compile does exactly what you are looking for. You can simply replace mvn install with mvn test-compile and you are done. No need to customise the pom file or anything. The below linked question is similar around #1:
Maven - How to compile tests without running them ?
mvn test-compile should be accepted as the best answer as Maven supports exactly what you want to do natively and without any magic. You would end up with this:
If I run mvn test-compile, I want all tests to compile, but I do not want to execute any.
If I run mvn test, I want all tests to compile, but execute only unit tests.
If I run mvn integration-test, I want to compile and execute all tests.
Don't specify the execution step(s) in the configuration of the failsafe plugin. E.g.
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
</plugins>
Now, you specifically need to call mvn failsafe:integration-test to run the integration tests; they will be skipped in other mvn targets.

Resources