【问题标题】:How to choose which JUnit5 Tags to execute with Maven如何选择使用 Maven 执行哪些 JUnit5 标签
【发布时间】:2017-07-14 06:50:17
【问题描述】:

我刚刚升级了我的解决方案以使用 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>

【问题讨论】:

标签: java maven junit junit5


【解决方案1】:

你可以这样使用:

<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)。

【讨论】:

  • 在 allTests 配置文件中留空 标记可确保运行所有测试,即使在代码中添加了新标记也是如此。
  • &lt;includeTags/&gt; 据我所知,这不是一个有效的配置选项。您介意提供文档参考吗?
  • 这个答案似乎确实是错误的,因为在surefire中没有&lt;includeTags&gt;,它应该是&lt;groups&gt;&lt;excludedGroups&gt;
【解决方案2】:

使用配置文件是可能的,但不是强制性的,因为 groupsexcludedGroups 是在 maven surefire plugin 中定义的用户属性,分别包含和排除任何 JUnit 5 标签(它也适用于 JUnit 4 和 TestNG测试过滤机制)。
因此,要执行带有slowfast 标记的测试,您可以运行:

mvn test -Dgroups=fast,slow

如果您想在 Maven 配置文件中定义排除和/或包含标签,则无需声明新属性来传达它们并在 maven surefire 插件中建立它们的关联。只需使用 maven surefire 插件定义和期望的 groups 和或 excludedGroups

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

【讨论】:

    【解决方案3】:

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

    <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" 来运行除所选类型之外的所有测试标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多