如何选择使用Maven执行哪些JUnit5标签

Dan*_*SFT 6 java junit maven junit5

我刚刚升级了我的解决方案以使用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>
Run Code Online (Sandbox Code Playgroud)

Nik*_*s P 12

你可以这样使用:

<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>2.19.1</version>
            <configuration>
                <properties>
                    <includeTags>${tests}</includeTags>
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

这样您就可以开始mvn -PallTests test所有测试(甚至是mvn -Dtests=fast,slow test).

  • 将all <tests>标记保留在allTests配置文件中可确保运行所有测试,即使在代码中添加了新标记也是如此. (2认同)
  • 这个答案似乎确实是错误的,因为 Surefire 中没有 `&lt;includeTags&gt;`,它应该是 `&lt;groups&gt;` 或 `&lt;excludedGroups&gt;` (2认同)

dav*_*xxx 7

使用配置文件是可能的,但它不是强制性的,groups并且maven surefire插件中excludedGroups定义的用户属性分别包含和排除任何JUnit 5标记(并且它也适用于JUnit 4和TestNG测试过滤机制). 因此,要执行标记测试或者你可以运行:
slowfast

mvn test -Dgroups=fast,slow
Run Code Online (Sandbox Code Playgroud)

如果要在Maven配置文件中定义排除和/或包含的标记,则无需声明新属性来传达它们,并在maven surefire插件中将它们关联起来.只需使用groups和或excludedGroups通过Maven的万无一失插件定义和期望:

<profiles>
    <profile>
        <id>allTests</id>
        <properties>
            <groups>fast,slow</groups>
        </properties>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)


sim*_*imo 5

您可以省略配置文件,只使用属性,这是更有弹性的方式。

<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>
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过键入进行快速测试mvn test、通过键入进行所有测试mvn test -Dtests=fast | slow或仅通过键入进行慢速测试mvn test -Dtests=slow。当您有更多测试标签时,您还可以通过键入 来运行除所选类型之外的所有测试标签mvn test -Dtests="! contract"