zoukankan      html  css  js  c++  java
  • junit5注解之顺序执行

    一、需求背景

    结合rest-asssured和junit5,对项目的增删改查实现了自动化测试,并且想让增删改查4个接口有序执行

    二、解决方案

    junit5的TestMethodOrder和@Order注解

    三、具体步骤

    1、官网定义

      用于为注解的测试类配置测试方法执行顺序; 类似于 JUnit 4 的 @FixMethodOrder。 这样的注释是继承的。

    默认情况下,将使用确定性但有意不明显的算法对测试方法进行排序。 这确保了测试套件的后续运行以相同的顺序执行测试方法,从而允许可重复的构建。

    虽然真正的单元测试通常不应该依赖于它们的执行顺序,但有时还是需要强制执行特定的测试方法执行顺序 — 例如,在编写集成测试或功能测试时,测试的顺序是 重要,尤其是与@TestInstance(Lifecycle.PER_CLASS) 结合使用。

    要控制测试方法的执行顺序,请使用 @TestMethodOrder 注释您的测试类或测试接口并指定所需的 MethodOrderer 实现。 您可以实现自己的自定义 MethodOrderer 或使用以下内置 MethodOrderer 实现之一。

    • DisplayName:根据显示名称按字母数字顺序对测试方法进行排序(请参阅显示名称生成优先规则)
    • MethodName:根据方法名称和形式参数列表按字母数字顺序对测试方法进行排序。
    • OrderAnnotation:根据通过 @Order 注释指定的值对测试方法进行数字排序。
    • Random:伪随机地订购测试方法并支持自定义种子的配置。
    • Alphanumeric:根据名称和形式参数列表按字母数字对测试方法进行排序。 不推荐使用 MethodName。 将在 6.0 中移除

    2、先在测试类上面添加注解,@TestMethodOrder(MethodOrderer.OrderAnnotation.class)

    3、在测试方法增加注解@Order(1)、@Order(2)...

    4、案例演示

      这里我们使用企业微信部门的增删改查接口来示范,实现了4个接口的自动化用例,只是想在功能回归测试中想让其顺序执行。

    package com.wechat.testcase;
    
    import io.qameta.allure.Description;
    import io.restassured.http.ContentType;
    import io.restassured.response.Response;
    import org.junit.jupiter.api.*;
    
    import static io.restassured.RestAssured.given;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    
    @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
    public class Demo_02 {
    
        public static String acessToken;
        public static String departmentId;
    
        @BeforeAll
        public static void getAccessToken(){
            acessToken = given()
                        .when()
                        .param("corpid","wwc376242756245a88")
                        .param("corpsecret","oQ2Q1pRsobYehKX6xYCJ44g9qhIaC_s16tZvCBxGg9Q")
                        .get(" https://qyapi.weixin.qq.com/cgi-bin/gettoken")
                        .then()
                        .log().body()
                        .extract()
                        .response()
                        .path("access_token")
                        ;
        }
    
    
        @Test
        @Description("创建部门")
        @DisplayName("创建部门")
        @Order(1)
        void createDepartment(){
            String creaetBody = "{
    " +
                    "   "name": "广州研发中心",
    " +
                    "   "name_en": "RDGZ",
    " +
                    "   "parentid": 1,
    " +
                    "   "order": 1,
    " +
                    "   "id": 2
    " +
                    "}
    ";
            Response creatResponse = given()
                    .contentType(ContentType.JSON)
                    .queryParam("access_token",acessToken)
                    .when()
                    .body(creaetBody)
                    .post("https://qyapi.weixin.qq.com/cgi-bin/department/create" )
                    .then()
                    .log().body()
                    .extract()
                    .response()
                    ;
    
            assertEquals("0",creatResponse.path("errcode").toString());
            departmentId = creatResponse.path("id")!=null ?creatResponse.path("id").toString() :null;
    
        }
    
    
    
        @Test
        @Description("更新部门")
        @DisplayName("更新部门")
        @Order(2)
        void updateDepartment(){
            String updateBody = "{
    " +
                    "   "id": "+departmentId+",
    " +
                    "   "name": "广州研发中心update",
    " +
                    "   "name_en": "RDGZupdate",
    " +
                    "   "parentid": 1,
    " +
                    "   "order": 1
    " +
                    "}
    ";
            Response updateResponse = given()
                    .contentType(ContentType.JSON)
                    .queryParam("access_token",acessToken)
                    .when()
                    .body(updateBody)
                    .post("https://qyapi.weixin.qq.com/cgi-bin/department/update" )
                    .then()
                    .log().body()
                    .extract()
                    .response()
                    ;
    
            assertEquals("0",updateResponse.path("errcode").toString());
    
        }
    
    
    
        @Test
        @Description("查询部门")
        @DisplayName("查询部门")
        @Order(3)
        void listDepartment(){
            Response listResponse = given()
                    .contentType(ContentType.JSON)
                    .param("access_token",acessToken)
                    .param("id",departmentId)
                    .when()
                    .get("https://qyapi.weixin.qq.com/cgi-bin/department/list")
                    .then()
                    .log().body()
                    .extract()
                    .response()
                    ;
    
            assertEquals("0",listResponse.path("errcode").toString());
        }
    
        @Test
        @Description("删除部门")
        @DisplayName("删除部门")
        @Order(4)
        void deleteDepartment(){
    
            Response deleteResponse = given()
                    .contentType(ContentType.JSON)
                    .param("access_token",acessToken)
                    .param("id",departmentId)
                    .when()
                    .get("https://qyapi.weixin.qq.com/cgi-bin/department/delete")
                    .then()
                    .log().body()
                    .extract()
                    .response()
                    ;
    
            assertEquals("0",deleteResponse.path("errcode").toString());
        }
    
    }

     5、测试结果

     四、总结

      junit5的执行顺序只能简单用于功能的自动化回归测试,虽然能帮助我们提高一定的效率,但是不能使用与整个工程的并发执行测试和单个测试用例的多次执行,需要在实际的自动化测试过程中需采用更加完善的测试方案。

     junti5顺序执行官网

    知道、想到、做到、得到
  • 相关阅读:
    素因子分解
    【转载】一张表看懂LTE和5G NR的区别
    看国家宝藏,顺便学习一下国密算法
    LTE-Advanced(4G)主要技术学习:CA、CoMp、HetNet
    未来移动通信的需求与挑战
    傅里叶级数
    正余弦函数的复指数表示
    网络基础——相关面试考点
    操作系统——相关面试考点
    小米2015笔试编程题
  • 原文地址:https://www.cnblogs.com/Durant0420/p/14988712.html
Copyright © 2011-2022 走看看