如何使用Maven排除除两个以外的所有测试文件?

时间:2019-05-31 19:19:25

标签: maven include maven-surefire-plugin

我喜欢做类似波纹管的东西,它将排除除两个测试文件以外的所有测试用例?

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.12.4</version>
      <configuration>
        <excludes>
            <exclude>**/*Test.java</exclude>
        </excludes>
        <includes>
            <include>path/to/Some1Test.java</include>
            <include>path/to/Some2Test.java</include>
        </includes>
      </configuration>
    </plugin>

这里的关键事实是我不想排除的测试文件,其命名方式也与我想排除的文件相同。提前致谢。

2 个答案:

答案 0 :(得分:0)

excludes赢得了includes的surefire插件配置(未记录,但以这种方式工作)。这是有道理的,因为应该将excludes视为例外情况,而将includes视为一般情况:您通常希望包括.....

但是在这里您想完全相反。
所以不要混用这两个元素,因为与您的实际conf一样:

  <configuration>
    <excludes>
        <exclude>**/*Test.java</exclude>
    </excludes>
    <includes>
        <include>path/to/Some1Test.java</include>
        <include>path/to/Some2Test.java</include>
    </includes>
  </configuration>

您将所有排除在外,因为excludes排除了所有此处(<exclude>**/*Test.java</exclude>),并且excludes赢得了includes

您只需要定义includes即可仅执行以下测试:

  <configuration>      
    <includes>
        <include>path/to/Some1Test.java</include>
        <include>path/to/Some2Test.java</include>
    </includes>
  </configuration>

答案 1 :(得分:0)

以下是带有示例的解决方案:

package com.example1;
import org.junit.Test;
public class TestClass1 {
    @Test
    public void test1() {
    }
}


package com.example1;
import org.junit.Test;

public class TestClass2 {
    @Test
    public void test2() {
    }
}

package com.example2;
import org.junit.Test;
public class TestClass1 {
    @Test
    public void test1() {
    }
}

package com.example2;
import org.junit.Test;
public class TestClass2 {
    @Test
    public void test2() {
    }
}

现在,以下是用于包含com.example1.TestClass1com.example1.TestClass2的surefire插件配置。在这里,除了这两个以外的其他测试类将不会执行。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <includes>
                        <include>com/example1/TestClass1.java</include>
                        <include>com/example1/TestClass2.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>