TERACytE
TERACytE

Reputation: 7863

Ignoring tests until called for in Maven/Junit?

Is there a way to setup a Maven project to run to ignore a collection of tests by default unless a specific system variable is present?

For example, if I run the following:

mvn clean install

A certain collection of tests are not executed as part of the build. If I execute the build in this way:

mvn clean install -DrunAllTests

The tests that are ignored by default in the standard build are also executed.

I know I can do this using TestNG, but I would prefer to stay with JUnit for now. I also tried using the Junit Categories feature, but could not get the tests to be ignored by default.

Any thoughts or ideas?

Upvotes: 0

Views: 256

Answers (2)

Stefan Birkner
Stefan Birkner

Reputation: 24520

You may use the Category feature of JUnit together with excludeGroups of Maven's Surefire plugin.

Upvotes: 2

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

To include tests for execution you can add the following configuration for maven-surefire-plugin:

<configuration>
   <includes>
      <include>**/SomeTests*.java</include>
   </includes>
</configuration>

So, you can use this together with Maven profiles, and extract/enable your additional tests in a spacial profile. For example:

<profiles>
  <profile>
    <id>allTests</id>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <includes>
            <include>**/SomeOtherTests*.java</include>
          </includes>
        </configuration>
     </plugins>
  </profile>
</profiles>

Then maven command would look like this:

mvn clean install -PallTests

If you prefer to use properties, you can add activation section to the allTests profile and activate it based on given property.

Upvotes: 4

Related Questions