测开学习笔记——接口自动化(九、Pytest数据驱动以及数据和代码分离、十、生成Allure测试报告)

目录

九、Pytest数据驱动以及数据和代码分离

1. 数据驱动:

2. 数据和代码分离

十、生成Allure测试报告


九、Pytest数据驱动以及数据和代码分离

1. 数据驱动:

 @pytest.mark.parametrize(args_name, args_value)
  • args_name:参数名,一般取caseinfo

  • args_value:列表或字典列表,并且列表中中有几个值就会执行几次

    • [a,b,c],[{},{},{},{ }]

2. 数据和代码分离

  1. 在用例文件

  •  class TestApi:
         @pytest.mark.parametrize("caseinfo",["百里”,"北凡"])
         def test_get_token(self,casetnfo):
             #获取参数
             title = caseinfo["title"]
             method = caseinfo["request"]["method"]
             url = caseinfo["request"]["url"]
             headers = caseinfo["request"]["headers"]
             params = caseinfo["request"]["params"]
             validate =caseinfo["validate"]
             #发送请求                             
             res = RequestUtil().send_all_reqest(method=method, url=url, params=params)     
             #断言
             if "access_token" in res.text:
                 #保存到文件,写入文件
                 yaml_util.write_yaml({"access_token":jsonpath.jsonpath(res.json(),"$.access_token")[0]}  
                 assert "access_token" in res.text
                      
  1. 在与用例文件同一个文件夹下-》新建一个get_token.yaml文件来保存生成的中间变量

  •  #正例
     - 
         title: 正确的获取到access_token鉴权码
         request:
             method: get
             url: https://api.weixin.qq.com/cgi-bin/token
             headers: None
             params: 
                 "grant_type": "client_credential"
                 "appid": "wx8a9de038e93f77ab"
                 "secret": "8326fc915928dee3165720c910effb86"
         validate: None
         
     #反例
     ​
     - 
         title: 验证grant_type必填
         request:
             method: get
             url: https://api.weixin.qq.com/cgi-bin/token
             headers: None
             params: 
                 "grant_type": ""
                 "appid": "wx8a9de038e93f77ab"
                 "secret": "8326fc915928dee3165720c910effb86"
         validate: None
         
         
     - 
         title: 验证appid必填
         request:
             method: get
             url: https://api.weixin.qq.com/cgi-bin/token
             headers: None
             params: 
                 "grant_type": "client_credential"
                 "appid": ""
                 "secret": "8326fc915928dee3165720c910effb86"
         validate: None
  1. 在yaml_util.py文件中写 读取测试用例的代码

 #读取测试用例
 def read_testcase(yaml_path):
     with open(yaml_path,encoding="utf-8",mode="r")as f:
         all_value = yaml.safe_load(f)
         return all_value
  1. 更改用例文件的路径

 class TestApi:
     @pytest.mark.parametrize("caseinfo",read_testcase("./testcases/gzh/get_token.yaml"))
     def test_get_token(self,casetnfo):
         print(caseinfo)

十、生成Allure测试报告

  1. 下载allure

  2. bin目录配置path路径

    • 在Dos和Pycharm窗口验证:allure --version

  3. 生成临时的json报告

    • 在pytest.ini配置

      addopts = -vs --alluredir=./temps --clean-alluredir

  4. 生成allure报告

    • 在run.py

       if _name_=='_main_':
           pytest.main()
           time.sleep(3)
           os.system("allure generate ./temps -o ./reports --clean")
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值