• httprunner(5)编写测试用例


    编写测试用例

    HttpRunner v3.x支持三种测试用例格式pytestYAMLJSON。官方强烈建议以pytest格式而不是以前的YAML/JSON格式编写和维护测试用例
    格式关系如下图所示:

     

    图文解析:

    1.postman收集的用例,curl命令行、抓包工具charles/fiddler生成的har,都转换为JSON格式
    2.Web平台中的JSON格式用例也被收集
    3.YAML文件与JSON格式相互转换
    4.最后将所有的JSON格式文件转换为py文件,用pytest去执行用例
    最后都是生成pytest去执行用例,之前就一直用的pytest框架,所以本人还是很喜欢httprunner的设计的
     

    用例结构

    上篇文章将百度的接口转换成pytest,如下:

    from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
    
    
    class TestCaseBaidu(HttpRunner):
    
        config = Config("testcase description").verify(False)
    
        teststeps = [
            Step(
                RunRequest("/")
                .get("https://www.baidu.com/")
                .with_headers(
                    **{
                        "Host": "www.baidu.com",
                        "Connection": "keep-alive",
                        "Cache-Control": "max-age=0",
                        "sec-ch-ua": '"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
                        "sec-ch-ua-mobile": "?0",
                        "Upgrade-Insecure-Requests": "1",
                        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36",
                        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                        "Sec-Fetch-Site": "none",
                        "Sec-Fetch-Mode": "navigate",
                        "Sec-Fetch-User": "?1",
                        "Sec-Fetch-Dest": "document",
                        "Accept-Encoding": "gzip, deflate, br",
                        "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
                        "Cookie": "PSTM=1610459846; BAIDUID=A40AD6AD806FBBED1033903732FFA453:FG=1; BD_UPN=123253; BIDUPSID=898B917A8EF92E036B0F06DC792638A1; __yjs_duid=1_c391922164c20246fd49e4ad821d46c41610613507042; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=33423_33513_33403_33273_33594_33585_26350_33568; delPer=0; BD_CK_SAM=1; PSINO=5; BAIDUID_BFESS=A40AD6AD806FBBED1033903732FFA453:FG=1; COOKIE_SESSION=86630_1_9_6_41_5_0_0_8_2_1_0_0_0_71_0_1612340974_1611912344_1612427533%7C9%23262013_235_1611912339%7C9; BD_HOME=1; ZD_ENTRY=baidu; BA_HECTOR=20ag2180aga18k21mm1g1pegu0r",
                    }
                )
                .with_cookies(
                    **{
                        "PSTM": "1610459846",
                        "BAIDUID": "A40AD6AD806FBBED1033903732FFA453:FG=1",
                        "BD_UPN": "123253",
                        "BIDUPSID": "898B917A8EF92E036B0F06DC792638A1",
                        "__yjs_duid": "1_c391922164c20246fd49e4ad821d46c41610613507042",
                        "BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
                        "H_PS_PSSID": "33423_33513_33403_33273_33594_33585_26350_33568",
                        "delPer": "0",
                        "BD_CK_SAM": "1",
                        "PSINO": "5",
                        "BAIDUID_BFESS": "A40AD6AD806FBBED1033903732FFA453:FG=1",
                        "COOKIE_SESSION": "86630_1_9_6_41_5_0_0_8_2_1_0_0_0_71_0_1612340974_1611912344_1612427533%7C9%23262013_235_1611912339%7C9",
                        "BD_HOME": "1",
                        "ZD_ENTRY": "baidu",
                        "BA_HECTOR": "20ag2180aga18k21mm1g1pegu0r",
                    }
                )
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal('headers."Content-Type"', "text/html;charset=utf-8")
            ),
        ]
    
    
    if __name__ == "__main__":
        TestCaseBaidu().test_start()
    

    每个用例都是HttpRunner的子类,且必须包含属性:config和teststeps。

    • config:配置测试用例,包含 base_url, verify, variables, export
    • teststeps:测试步骤的列表,每个步骤都对应一个API请求或调用另一个测试用例,此外还支持variables/extract/validate/hooks创建极其复杂的测试用例
       

    链式调用

    HttpRunner v3.x最重要的功能之一就是支持链式调用,使用链式调用,不需要记住任何测试用例的信息,在IDE中编写测试用例时就可以智能完成

  • 相关阅读:
    多测师讲解jmeter _ 导入本地文本内容参数化方法一__(1)高级讲师肖sir
    深圳_多测师面试 _平安项目总结(2020年10月)_高级讲师肖sir
    多测师讲解jmeter _token提取_高级讲师肖sir
    多测师讲解jmeter _图片详解_(全)高级讲师肖sir
    深圳精英面试总结——华为外包面试,明源云客,有咖面试,招商面试 总结(001)
    多测师讲解接口测试 —jmeter接数据库(004)_高级讲师肖sir
    多测师讲解jmeter _接口请求_(003)高级讲师肖sir
    多测师讲解jmeter _安装和配置环境(00)_高级讲师肖sir
    多测师讲解接口 _需求文档(用户增删改查)_高级讲师肖sir
    jemeter参数化读取文件
  • 原文地址:https://www.cnblogs.com/jiakecong/p/14379033.html
Copyright © 2020-2023  润新知