Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Maven surefire plugin to include tests

I am using Maven to build my project. I currently split testing into different hierarchies:

  • Unit tests -> src/test/java/**/*Test.java
  • Integration tests -> src/test-integration/java/**/*Test.java
  • External tests -> src/test-external/java/**/*Test.java

Here is my maven-surefire-plugin section:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <configuration>
        <includes>
            <include>src/test/java/**/*Test.java</include>
        </includes>
    </configuration>
</plugin>

The <include> directive above does not work. No tests are executed when I run: mvn clean test

I tried **/*Test.java and it runs all the tests -- unit, integration, and external. However, for the default test suite, I only want to run the unit tests.

How can I make this work in Maven?

Ref:

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
like image 560
kevinarpe Avatar asked Dec 19 '13 04:12

kevinarpe


People also ask

Why Maven surefire plugin tests are skipped?

When the Surefire plugin reaches the test goal, it will skip the unit tests if the maven. test. skip properties is set to true . Another way to configure Maven to skip unit tests is to add this configuration to your project's pom.

What is the purpose of surefire plugin?

The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats: Plain text files ( *. txt )

Which Maven plugin triggers the test case?

How to run all the test files and trigger test using maven-surefire plugin.


3 Answers

You should separate your unit and integration test cases. Unit tests can be run using Surefire plugin and there is a separate plugin called Failsafe for running integration tests.

Surefire plugin by default runs all test files whose name ends with *Test.java. The trick is to name your integration test files with a different name, say *IT.java. Failsafe plugin will identify them as integration tests.

You can find a sample usage in this answer - How do I get my Maven Integration tests to run

Also you dont have to separately configure Surefire plugin if you follow the default setup like putting your test case files in src/test/java folder and name your test files as *Test.java.

like image 156
maheshsenni Avatar answered Oct 17 '22 08:10

maheshsenni


This is because <include> path is relative to <testSourceDirectory> which defaults to ${project.build.testSourceDirectory} = src/test/java. Try this

<include>**/*Test.java</include>
like image 9
Evgeniy Dorofeev Avatar answered Oct 17 '22 06:10

Evgeniy Dorofeev


Would it be easier to use an exclusion rather than the inclusion?

<excludes>
    <exclude>test-integration/**/*</exclude>
    <exclude>test-external/**/*</exclude>
</excludes>

Or something like that?

like image 1
EdH Avatar answered Oct 17 '22 06:10

EdH