微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

目录

简介

前面提到,使用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-@R_560_404[email protected]</version>
</dependency>

创建 .groovy 文件,运行


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

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

总结

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐