【问题标题】:JUnit5 run tests by tagsJUnit5 通过标签运行测试
【发布时间】:2018-07-21 13:03:32
【问题描述】:

我有一个设置Maven+JUnit5+Selenium,我的pom.xmlhttps://github.com/13Dima13/G/blob/master/pom.xml

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <forkCount>4</forkCount>
                <reuseForks>false</reuseForks>
                <properties>
                    <includeTags>${tag}</includeTags>
                </properties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junit.platform.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-logger-api</artifactId>
                    <version>${surefire-logger-api}</version>
                </dependency>
            </dependencies>
        </plugin>

首先我认为@Tag 工作正常,因为当我给我的班级添加注释时https://github.com/13Dima13/G/blob/master/src/test/java/com/example/project/TestThreads2.java

@Test
@Tag("smoke")
public void test1() {
    open("https://mvnrepository.com/artifact/log4j/log4j/1.2.17");
    $("#maincontent > table > tbody > tr:nth-child(1) > th").waitUntil(Condition.appears, 120000);
    $("#maincontent > table > tbody > tr:nth-child(1) > th").shouldBe(Condition.text("License"));
    assertEquals("Maven Repository: log4j » log4j » 1.2.17", title());
    out.println("test1 Passed");
}

然后从终端运行

mvn test -Dtag=smoke

它只执行标记为@Tag("smoke") 的测试,所以在我的例子中只有一个测试,这让我很高兴。

但是 当我开始在我的实际项目中使用它时,我意识到它有时不起作用。

例如,如果我的带有测试方法的Test.class 没有放在父项目文件夹中(例如在项目文件夹的子文件夹中)https://github.com/13Dima13/G/blob/master/src/test/java/com/example/project/test2/GoogleSearch.java,则注释根本不起作用。

所以注释@Tag 不适用于整个项目还是我错过了什么?

好例子https://ibb.co/gjbSZJ

【问题讨论】:

    标签: maven selenium annotations tags junit5


    【解决方案1】:

    mvn test -D groups=smoke

    试一试……

    或者,如果您希望快速查看结果(并且不需要重新编译代码),请选择:

    mvn surefire:test -D groups=smoke

    请注意,-D 后面的空格是可选的,纯粹是出于美观的原因。

    注意我对 java/maven 世界也很陌生,但它的价值:到目前为止,我从来没有让它与 -D tag 一起工作。很想知道这是怎么回事(cmets 链接到解释?)

    【讨论】:

      【解决方案2】:

      第一个猜测:您的测试类的名称 (test3.java) 与 maven 测试类的默认模式不匹配,因此将被忽略。

      【讨论】:

      【解决方案3】:

      JUnit 5 的支持包含在Maven Surefire Plugin Version 2.22.0

      <dependencies>
          [...]
          <dependency>
              <groupId>org.junit.jupiter</groupId>
              <artifactId>junit-jupiter-engine</artifactId>
              <version>5.2.0</version>
              <scope>test</scope>
          </dependency>
          [...]
      </dependencies>
      

      也许你需要像这样定义 maven-surefire-plugin:

      <build>
          <plugins>
              ...
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>2.22.0</version>
                  <configuration>
                      <excludes>
                          <exclude>some test to exclude here</exclude>
                      </excludes>
                  </configuration>
              </plugin>
          </plugins>
      </build>
      

      因此您的配置可以简化。

      【讨论】:

        猜你喜欢
        • 2017-07-12
        • 1970-01-01
        • 2018-05-29
        • 1970-01-01
        • 1970-01-01
        • 2018-04-11
        • 1970-01-01
        • 2022-07-23
        • 2021-12-29
        相关资源
        最近更新 更多