标记用例tag

发布时间 2023-08-25 21:42:42作者: Mr-v
  • 通过 Tag 对用例分组:
    • 环境分组: 测试环境、预发布环境
    • 阶段分组: 冒烟用例
    • 版本分组: V1.1、V1.2
  • 设置标签
  • 根据标签执行
    • 结合 Maven 执行
    • 结合测试套件执行
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
public class TagExampleTest {
    @Tag("preprod")
    @Test
    void test1(){
        System.out.println("预生产环境");
    }
    @Tag("test")
    @Test
    void test2(){
        System.out.println("测试环境");
    }
    @Tag("dev")
    @Test
    void test3(){
        System.out.println("开发环境");
    }
    @Tag("dev")
    @Tag("test")
    @Test
    void test4(){
        System.out.println("开发+测试环境");
    }
}

 

Maven 结合 tag 构建-修改 pom 文件

  • groups 表示执行包含标签或者标签表达式的用例。
  • excludedGroups 表示不执行包含该标签或者标签表达式的用例。
  • 使用命令 mvn clean test 执行用例
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
            <!-- 要执行的标签 -->
                <groups>test</groups>
            <!-- 不要执行的标签 -->
                <excludedGroups>dev</excludedGroups>
            </configuration>
        </plugin>
    </plugins>
</build>
  • 注意: 如果使用命令行的同时也配置了 pom 文件, pom 的配置优先级更高

# 执行 test 标签的用例 mvn clean test -Dgroups="test"

# 执行不含test 标签的用例 mvn clean test -DexcludedGroups="test"

Tags 的命名规范

  • 不准为空。
  • 标签不得包含空格。
  • 标签不得包含 ISO 控制字符。
  • 标签不得包含以下任何保留字符
    • ,
    • ()
    • &
    • |
    • !

Tag 表达式