使用Maven过滤带有注释@Tag(“ name_test”)的JUnit 5测试用例

时间:2019-01-14 20:48:00

标签: java maven maven-surefire-plugin junit5

我想过滤Junit 5测试用例,我正在使用注释@Tag(“ type_test”)。我使用命令“ mvn -Dtests = a test”使用maven运行测试,但是它将运行所有情况。例如,我有两个方法,我只想运行带有@Tag(“ a”)的方法,但是控制台中的结果是“ Hello word 1”和“ Hello word 2”。请参阅示例代码。


    static Properties properties = null;

    @BeforeAll
        public static void setUp() throws Throwable {
        properties = CommonMethods.loadConfigPropertiesFile();
        RestAssured.baseURI = properties.getProperty("BASE_HOST");
    }

    @Test
    @Tag("a")
    public void test1() {
        System.out.println("hello word 1");
    }   


    @Test
    @Tag("b")
    public void test2() {
        System.out.println("hello word 2");
    }
}

pom.xml

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <properties>
                        <includeTags>${tests}</includeTags>
                    </properties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.0-M2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M2</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0-M2</version>
        </dependency>
    </dependencies>


3 个答案:

答案 0 :(得分:1)

您使用的版本和库已过时。请重试:

  • Maven Surefire 2.22.1(更好:3.0.0-M3)
  • JUnit Jupiter 5.3.2(更好:5.4.0-M1)

请参阅此示例pom.xml文件,该文件还介绍了如何过滤标记:

<build>
    <plugins>
        <!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <groups>a</groups>
                <!-- excludedGroups>slow</excludedGroups -->
            </configuration>
        </plugin>
    </plugins>
</build>

来源:https://github.com/junit-team/junit5-samples/blob/master/junit5-migration-maven/pom.xml

有关如何使用Maven配置JUnit平台的更多详细信息,请参见《 JUnit 5用户指南》 https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven或Maven Surefire文档:https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

答案 1 :(得分:0)

我找到了解决方案。重要的是验证并尝试每个依赖项的最新版本。在此示例中:

maven-surefire-plugin(3.0.0-M3) junit-platform-surefire-provider(1.3.0-M1) junit-jupiter-engine-(5.4.0-M1) junit-jupiter-api-(5.4.0-M1)

解决方案

没有配置文件:

pom.xml

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
            <configuration>
                <properties>
                    <includeTags>${tests}</includeTags>
                </properties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.3.0-M1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0-M1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.0-M1</version>
    </dependency>
</dependencies>

您可以使用命令“ mvn test -Dtests = a”仅执行带有注释@Tag(“ a”)的方法

具有配置文件:

在pom.xml中添加此示例代码

<profiles>
    <profile>
        <id>serverdevelop</id>
        <properties>
            <tests>develop</tests>
        </properties>
    </profile>
    <profile>
        <id>servertesting</id>
        <properties>
            <tests>testing</tests>
        </properties>
    </profile>
    <profile>
        <id>serverproduction</id>
        <properties>
            <tests>production</tests>
        </properties>
    </profile>
</profiles>

例如,您可以使用命令“ mvn test -Pserverdevelop”仅执行带有注释@Tag(“ develop”)的方法。这对于按环境分离测试用例非常有用。

答案 2 :(得分:0)

无需触摸您的POM文件。

mvn surefire:test -D groups=a,b也可以解决问题。

如果您需要在运行测试之前进行编译,请使用:

mvn test -D groups=a,b

请注意,-Dgroups=a,b之间的空格是可选的,纯粹是出于装饰性原因。