java-Surefire不排除标有@Tag的JUnit 5测试

我有一个使用两种测试方法的JUnit 5测试.一个标记为“ slow”,另一个标记为“ fast”.我还用排除标签配置了SureFire插件,使它应该忽略带有“ slow”标签的测试.

我希望通过此设置仅执行带有“ fast”标签的测试,但就我而言,两者都是.看来@Tag注释根本没有任何作用.

这是测试类:

package junit5;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

class TaggingTest {

    @Tag("fast")
    @Test
    void fast() {
        System.out.println("*** fast ***");
    }

    @Tag("slow")
    @Test
    void slow() {
        System.out.println("*** slow ***");
    }

}

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>junit5</groupId>
    <artifactId>tagging</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <properties>
                        <excludeTags>slow</excludeTags>
                    </properties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

当我运行mvn test时,我得到以下输出:

> mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building tagging 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ tagging ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ tagging ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ tagging ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/olaf.prins/temp/junit-tagging/tagging/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ tagging ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/olaf.prins/temp/junit-tagging/tagging/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.19:test (default-test) @ tagging ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running junit5.TaggingTest
*** fast ***
*** slow ***
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.049 sec - in junit5.TaggingTest

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.800 s
[INFO] Finished at: 2017-11-15T16:36:14+01:00
[INFO] Final Memory: 13M/512M
[INFO] ------------------------------------------------------------------------

如您所见,两个测试用例都已运行.

据我了解的文档,我已经正确设置了所有内容.我想念什么?

解决方法:

此问题是由在JDK 9上运行的maven-surefire-plugin中的错误引起的:https://issues.apache.org/jira/browse/SUREFIRE-1445

上一篇:java – 如果没有至少一个TestEngine,则无法创建Launcher;考虑在Junit 5中将引擎实现JAR添加到类路径中


下一篇:java-三个测试容器来自哪里?