使用Maven Command运行Junit Suite

Jav*_* SE 33 junit maven-2 test-suite maven

我有多个Junit测试套件(SlowTestSuite,FastTestSuite等).我想使用maven命令只运行特定的套件.例如

mvn clean install test -Dtest=FastTestSuite -DfailIfNoTests=false
Run Code Online (Sandbox Code Playgroud)

但它不起作用.根本就没有运行任何测试.请给我任何建议.

Jav*_* SE 42

我通过在pom中添加属性来实现这一点:

<properties>
    <runSuite>**/FastTestSuite.class</runSuite>
</properties>
Run Code Online (Sandbox Code Playgroud)

和maven-surefire-plugin应该是:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>${runSuite}</include>
                </includes>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

所以它默认意味着它将运行FastTestSuite但你可以使用maven命令运行其他测试,例如SlowTestSuite,如下所示:

mvn install -DrunSuite=**/SlowTestSuite.class -DfailIfNoTests=false
Run Code Online (Sandbox Code Playgroud)

  • 谢谢**@ Java SE**,您的方法与*Maven 3.3.3*和*JUnit 4.12*完美配合.但是可以使用`**/FastTestSuite.java`代替`**/FastTestSuite.class`.两种变体都表现良好,但指定`.java`看起来更符合官方文档(例如,http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html). (2认同)

Jea*_*evy 5

您错过的关键字是maven-surefire-plugin:http://maven.apache.org/plugins/maven-surefire-plugin/.

用法是:

<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>
Run Code Online (Sandbox Code Playgroud)

如果您对堆栈溢出进行一点搜索,您可能会发现以下信息:

使用maven-failsafe-plugin在Maven中运行JUnit4测试套件 使用带有Maven Failsafe插件的JUnit类别

此外,您可以定义配置文件,如fastTest,将通过向cmd行添加参数来触发:

mvn package -PfastTests
Run Code Online (Sandbox Code Playgroud)

此配置文件也包含一些内容.

  • 实际上,配置文件管理是触发不同行为的方式. (2认同)