【问题标题】:Filter JUnit 5 test cases with the annotation @Tag("name_test") using Maven使用 Maven 使用注释 @Tag("name_test") 过滤 JUnit 5 测试用例
【发布时间】:2019-01-14 20:48:00
【问题描述】:

我想过滤我的 Junit 5 测试用例,我正在使用注解 @Tag("type_test")。我使用命令mvn -Dtests=a test 使用maven 运行我的测试,但它运行所有情况。例如,我有两个方法,我只想运行 @Tag("a") 的方法,但控制台中的结果是“Hello word 1”和“Hello word 2”。请参阅示例代码。


    static Properties properties = null;

    @BeforeAll
        public static void setUp() throws Throwable {
        properties = CommonMethods.loadConfigPropertiesFile();
        RestAssured.baseURI = properties.getProperty("BASE_HOST");
    }

    @Test
    @Tag("a")
    public void test1() {
        System.out.println("hello word 1");
    }   


    @Test
    @Tag("b")
    public void test2() {
        System.out.println("hello word 2");
    }
}

pom.xml

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <properties>
                        <includeTags>${tests}</includeTags>
                    </properties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0-M2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.0-M2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M2</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0-M2</version>
        </dependency>
    </dependencies>


【问题讨论】:

    标签: java maven maven-surefire-plugin junit5


    【解决方案1】:

    您使用的版本和库已过时。请重试:

    • Maven Surefire 2.22.1(更好:3.0.0-M3)
    • JUnit Jupiter 5.3.2(更好:5.4.0-M1)

    请参阅此示例 pom.xml 文件,其中还介绍了如何过滤标签:

    <build>
        <plugins>
            <!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <groups>a</groups>
                    <!-- excludedGroups>slow</excludedGroups -->
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    来源:https://github.com/junit-team/junit5-samples/blob/master/junit5-migration-maven/pom.xml

    有关如何使用 Maven 配置 JUnit 平台的更多详细信息,请参阅 JUnit 5 用户指南 https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven 或 Maven Surefire 文档:https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

    【讨论】:

    • 在这种情况下,解决方案是放置每个依赖项的最新版本。 maven-surefire-plugin = 3.0.0-M3,junit-platform-surefire-provider = 1.3.0-M1,junit-jupiter-engine = 5.4.0-M1,junit-jupiter-api = 5.4.0-M1。我继续使用配置 ${tests} 的相同 pom.xml,我可以使用“mvn test -Dtests=a”执行并只收到消息“hello word 1”
    • 请注意,“junit-platform-surefire-provider”已被弃用和停产——因此切换到 Surefire 内置支持更具前瞻性。
    • 3.0.0-M3稳定吗?这个后缀让我很生气。我没有找到任何关于 maven 核心插件的版本方案的信息
    • 这里至少列出了 M3 中修复的内容:maven.apache.org/surefire/maven-surefire-plugin
    【解决方案2】:

    无需修改您的 POM 文件。

    mvn surefire:test -D groups=a,b 也应该可以解决问题。

    如果您需要在运行测试之前进行编译,请使用:

    mvn test -D groups=a,b

    请注意,-Dgroups=a,b 之间的空格是可选的,纯粹是为了美观。

    【讨论】:

      【解决方案3】:

      我找到了解决方案。重要的是验证并尝试使用每个依赖项的最新版本。在这个例子中:

      • maven-surefire-plugin (3.0.0-M3)
      • junit-platform-surefire-provider (1.3.0-M1)
      • junit-jupiter-engine (5.4.0-M1)
      • junit-jupiter-api (5.4.0-M1)

      解决方案

      没有个人资料:

      pom.xml

      <build>
          <plugins>
              <plugin>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>3.0.0-M3</version>
                  <configuration>
                      <properties>
                          <includeTags>${tests}</includeTags>
                      </properties>
                  </configuration>
                  <dependencies>
                      <dependency>
                          <groupId>org.junit.platform</groupId>
                          <artifactId>junit-platform-surefire-provider</artifactId>
                          <version>1.3.0-M1</version>
                      </dependency>
                  </dependencies>
              </plugin>
          </plugins>
      </build>
      <dependencies>
          <dependency>
              <groupId>org.junit.jupiter</groupId>
              <artifactId>junit-jupiter-engine</artifactId>
              <version>5.4.0-M1</version>
          </dependency>
          <dependency>
              <groupId>org.junit.jupiter</groupId>
              <artifactId>junit-jupiter-api</artifactId>
              <version>5.4.0-M1</version>
          </dependency>
      </dependencies>
      

      您可以使用命令mvn test -Dtests=a 只执行带有注解@Tag("a") 的方法

      个人资料:

      在 pom.xml 中添加此示例代码

      <profiles>
          <profile>
              <id>serverdevelop</id>
              <properties>
                  <tests>develop</tests>
              </properties>
          </profile>
          <profile>
              <id>servertesting</id>
              <properties>
                  <tests>testing</tests>
              </properties>
          </profile>
          <profile>
              <id>serverproduction</id>
              <properties>
                  <tests>production</tests>
              </properties>
          </profile>
      </profiles>
      

      例如,您可以使用命令mvn test -Pserverdevelop 仅执行带有注释@Tag("develop") 的方法。这对于按环境分离测试用例非常有用。

      【讨论】:

      • a) 您正在混合不同版本的 JUnit Platform aritfact(此处为 1.3 和 1.4),这会在不久的将来带来麻烦......和 ​​b) 自定义 junit-platform-surefire-provider,它最初由 JUnit 团队开发,已被弃用,并计划在 JUnit Platform 1.4 中删除。请改用 Maven Surefire 的原生支持。详情见junit.org/junit5/docs/current/user-guide/…
      • 但是,如果我删除了依赖项 junit-platform-surefire-provider 不起作用。我运行“mvn test -Dtests = a”,我的所有案例都执行错误。
      • 请将此报告给 Surefire 插件跟踪器,闻起来像一个错误:issues.apache.org/jira/projects/SUREFIRE/issues
      • 从 5.4 开始,如果你想使用 Junit 5,junit-jupiterpom.xml 中唯一需要的依赖项。它将拉入 junit-jupiter-enginejunit-jupiter-apijunit-jupiter-params 等根据需要。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多