gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:33 26 4
gpt4 key购买 nike

我有一个 JUnit 5 测试,有两种测试方法。一个用标签“慢”注释,另一个用标签“快”注释。我还使用 excludeTags 配置了 SureFire 插件,这样它就可以忽略带有“slow”标签的测试。

我希望使用此设置只会执行带有“快速”标签的测试,但在我的情况下两者都是。 @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] ------------------------------------------------------------------------

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

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

最佳答案

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

关于java - Surefire 不排除标有@Tag 的 JUnit 5 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47311801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com