如何在默认情况下排除特定测试,但如果激活某个配置文件,请包括它们?

时间:2013-06-14 03:23:29

标签: java maven

现在我知道如果我使用

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

我将能够跳过我不想运行的测试。但是,我希望有一个特定的配置文件,如果我使用该特定配置文件构建上述排除的测试将运行。我试过了

<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>

与上述相似,但似乎不起作用。

2 个答案:

答案 0 :(得分:0)

您可以使用属性定义测试源目录,并使用自定义配置文件覆盖它,如下所示:

<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>

这样,执行mvn test将在 src / test / java 中运行所有测试,而mvn test -Pbenchmark将在 src / benchmark-tests /中运行测试的java

控制台输出:

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

和:

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

答案 1 :(得分:0)

Maven正在将您的配置合并到一起,而不是按照您的预期进行覆盖,因此您对配置文件处于活动状态的最终配置将如下所示:

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

不需要所有默认<include>。他们是already there anyway。只需将<excludes/>告诉Maven你想要停止在基本POM中指定的排除。即,在您的个人资料中,只需说:

<configuration>
    <excludes/>
</configuration>