yaml接口自动化实战(2)

yaml_read.py

import yaml
class YamlUtil:
    def __init__(self,yaml_file):
        """
        通过 init方法把yaml文件传入到这个类
        :param yaml_file
        """
        self.yaml_file=yaml_file

    def read_yaml(self):
        """
        读取yaml,对yaml反序列化,就是把yaml格式转换成dict格式
        :return:
        """
        with open(self.yaml_file,encoding="utf-8") as f:
            value=yaml.load(stream=f, Loader=yaml.FullLoader)
            return value
if __name__ == '__main__':
    YamlUtil('test_demo.yaml').read_yaml()

test.api.yaml

#用例一
-
  name: 获得token鉴权码的接口
  request:
    #url: "https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo"
    url: https://api.weixin.qq.com/cgi-bin/token
    method: get
    headers:
      Content-Type: application/json
    params:
      grant_type: client_credential
      #action: showinfo
      appid: wxa6db779e647d7e57
      secret: ad0e243c8033b2e1e3c2d144611eee82
  validate:
    - eq: {expries_in: 7200}    #注意括号不能去掉,去掉了成为数组
#    - eq:
#        - expries_in: 7200 #变成数组要多解析一层 和上面方法一致

#用例二
-
  name: 获得token鉴权码的接口
  request:
    #url: "https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo"
    url: https://api.weixin.qq.com/cgi-bin/token
    method: get
    headers:
      Content-Type: application/json
    params:
      #action: showinfo
      appid: wxa6db779e647d7e57
      secret: ad0e243c8033b2e1e3c2d144611eee82
  validate:
    - eq: {expries_in: 7200}   #注意括号不能去掉,去掉了成为数组

test.api.py

import requests
import pytest
from testcase.yaml_demo import YamlUtil
import os

class TestApi(object):
    @pytest.mark.parametrize("args",YamlUtil('./test_api.yaml').read_yaml())
    def test_01_qianghong(self,args):
        # print(args)
        url=args['request']['url']
        params=args['request']['params']
        # params={
        #     "action":"showinfo"
        #     "appid""wxa6db779e647d7e57",
        #     "secret":"ad0e243c8033b2e1e3c2d144611eee82"
        #          }
        # #
        # url = 'https://api.weixin.qq.com/cgi-bin/token'
        # params={
        #     "grant_type":"client_credential",
        #     "appid":"wxa6db779e647d7e57",
        #     "secret":'ad0e243c8033b2e1e3c2d144611eee82'
        # }
        res=requests.get(url=url,params=params)
        print(res.text)
        #assert args['validate']['eq']['expries_in'] in res #无法使用此断言方式 反例无法通过  参考断言的封装
if __name__ == '__main__':
    pytest.main([r'X:\pytest_pro\testcase\test_api.py'])
    #os.system("allure generate ./temp -o ./report/ --clean")

这个demo数据驱动太粗糙,只是个锥形
数据驱动
1、还有断言的封装
2、allure 报告的定制
3、关键字驱动和数据驱动结合实现接口自动化测试
4、python的反射 :
---------------------正常写法:先初始化对象再调方法 反射写法:通过对象得到类对象,然后通过类对象调用方法
5、jenkins的持续集成和allure报告集成,并且根据自动化的错误率来发送电子邮件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值