goh
goh

Reputation: 29511

How do I exclude specific tests by default but include them if activate a a certain profile?

Right now I am aware that if I use

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/Benchmark*.java</exclude>
        </excludes>
    </configuration>
</plugin>

I will be able to skip the tests I don't want to run. However, I want to have a specific profile whereby the above excluded tests will run if i build using that particular profile. I've tried

<profiles>
    <profile>
        <id>benchmark</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <includes>
                            <include>**/Test*.java</include>
                            <include>**/*Test.java</include>
                            <include>**/*TestCase.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

along with the above but it seems like it doesn't work.

Upvotes: 2

Views: 1279

Answers (2)

Ryan Stewart
Ryan Stewart

Reputation: 128829

Maven is merging your configurations together instead of overwriting as you expect it to, so your final configuration with the profile active would look like:

<configuration>
    <includes>
        <include>**/Test*.java</include>
        <include>**/*Test.java</include>
        <include>**/*TestCase.java</include>
    </includes>
    <excludes>
        <exclude>**/Benchmark*.java</exclude>
    </excludes>
</configuration>

There's no need for all those default <include>s. They're already there anyway. Just put <excludes/> to tell Maven you want to stop doing the excludes that you specified in the basic POM. I.e., in your profile, just say:

<configuration>
    <excludes/>
</configuration>

Upvotes: 2

Marlon Bernardes
Marlon Bernardes

Reputation: 13853

You could define your test source directory using a property, and override it using a custom profile, like this:

<properties>
    <test.dir>src/test/java</test.dir>
</properties>

<profiles>
    <profile>
        <id>benchmark</id>
        <properties>
            <test.dir>src/benchmark-tests/java</test.dir>
        </properties>
    </profile>
</profiles>

<build>
    <testSourceDirectory>${test.dir}</testSourceDirectory>
</build>

This way, executing mvn test would run all your tests inside src/test/java and mvn test -Pbenchmark would run the tests inside src/benchmark-tests/java.

Console output:

mvn clean test
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running br.com.instaweb.sample.SampleUnitTest
sample unit test was run
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec

and:

mvn clean test -Pbenchmark
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running br.com.stackoverflow.BenchmarkTest
benchmark test was run

Upvotes: 1

Related Questions