首页 > 其他 > 详细

原创:使用testng+spock 处理传参

时间:2021-04-16 17:58:26      阅读:29      评论:0      收藏:0      [点我收藏+]

简介

前面提到,使用cucumber的feature文件来组织测试数据,使用起来不是很方便,还有一种更好的方法来推荐,就是使用spock + groovy 来处理传参数

spock + groovy 介绍

feature文件基本格式

  • Given: 假设
  • when: 执行操作
  • expect: 验证
  • where : 参数文件

举一个例子


import com.alibaba.fastjson.JSON
import com.project.auto.aircraft.v5openapi.V5OpenApiFunctions
import io.restassured.response.Response
import org.testng.annotations.Test
import spock.lang.Specification

class Calc {
    public static int add (int a , int b){
        return a + b;
    }
}

class demoTest extends Specification{

    @Test
    def "test1"(){
        expect:
        Calc.add(a as int, b as int) == result

        where:
                a   |   b   |   result
                1   |   1   |   2
                2   |   4   |   6
                3   |   4   |   6
    }

    @Test
    def "getPublicPriceLimitTest"(){

        /**
         * 已经调试通过了
         */

        given:
        def map1 = ["instId":instId];
        expect:
        Response resp = V5OpenApiFunctions.getPublicPriceLimitV5(map1);
        String respBody = resp.getBody().prettyPrint();
        JSON.parseObject(respBody).get("code").equals(code as String);

        where:
        id  |   description            |instId            |code
        6   |   "异常测试-无效instId"      |"errorBTC-USD-210326-2000-C"| 51000
    }
}
 

spock 依赖


<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <version>1.3-groovy-2.5</version>
</dependency>

创建 .groovy 文件,运行


生成上面的 groovy 文件,例如命名为 test.groovy 文件

右键选择 文件中的 class demoTest ,然后 run

总结

  1. given,when, then 文件结构,逻辑非常清楚

  2. 测试用例放在 where 块中,用例和逻辑分离

  3. 测试用例,逻辑,都放在一个类中来维护,易于维护,相当高级

  4. 处理参数和逻辑的最好的方式

原创:使用testng+spock 处理传参

原文:https://www.cnblogs.com/liang6/p/14666860.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!