如何使用Maven选择要执行的JUnit5标签

24

我刚刚升级了我的解决方案,使用JUnit5。现在我正在尝试为我的测试创建两个标签@Fast@Slow。首先,我使用下面的Maven条目配置默认构建中要运行的测试。这意味着当我执行mvn test时,只会执行我的快速测试。我假设我可以使用命令行覆盖此设置。但是,我无法弄清楚该输入什么来运行我的慢速测试....

我假设像这样... mvn test -Dmaven.IncludeTags=fast,slow,但它不起作用。

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <properties>
            <includeTags>fast</includeTags>
            <excludeTags>slow</excludeTags>
        </properties>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.0-M3</version>
        </dependency>
    </dependencies>
</plugin>

我的代码也不起作用。我在githup上的代码链接如下: https://github.com/anhtv08/junit5-splitting-test - Joey Trang
3个回答

26

使用配置文件是一种可能性,但并非必须,因为groupsexcludedGroups是用户属性,在Maven Surefire插件中定义,用于分别包括和排除任何JUnit 5标签(它还可以与JUnit 4和TestNG测试过滤机制一起使用)。 因此,要执行带有slowfast标记的测试,可以运行:

mvn test -Dgroups=fast,slow

如果你想在Maven配置文件中定义要排除或者包含的标签,你不需要声明一个新的属性来传达它们,并在maven-surefire插件中进行关联。只需使用由maven-surefire插件定义和期望的groupsexcludedGroups即可:

<profiles>
    <profile>
        <id>allTests</id>
        <properties>
            <groups>fast,slow</groups>
        </properties>
    </profile>
</profiles>

23

您可以使用这种方式:

<properties>
    <tests>fast</tests>
</properties>

<profiles>
    <profile>
        <id>allTests</id>
        <properties>
            <tests>fast,slow</tests>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <groups>${tests}</groups>
            </configuration>
        </plugin>
    </plugins>
</build>

您可以使用mvn -PallTests test命令运行所有测试(或者使用mvn -Dtests=fast,slow test命令只运行快速/慢速测试)。


2
在 allTests 配置文件中留空 <tests> 标签可以确保运行所有测试,即使代码中添加了新标签。 - Marcin Kunert
1
据我所知,<includeTags/>不是有效的配置选项。您能提供文档参考吗? - btiernay
2
这个答案似乎是错误的,因为 surefire 中没有 <includeTags>,正确的应该是 <groups><excludedGroups> - Ghilteras
https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html#Filtering_by_Tags - user482745

5

您可以省略profile并直接使用properties,这是更加灵活的方式。

<properties>
    <tests>fast</tests>
</properties>

<build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <groups>${tests}</groups>
                </configuration>
            </plugin>
        </plugins>
    </build>

然后,您可以通过输入mvn test来运行快速测试,通过输入mvn test -Dtests=fast | slow来运行所有测试或仅运行缓慢的测试,通过输入mvn test -Dtests=slow来运行缓慢的测试。如果您有更多的测试标签,那么您也可以通过输入mvn test -Dtests="! contract"来运行除所选类型之外的所有测试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接