原接口测试用例
import pytest import requests class TestApi: def test_01_huahua(self): url = 'https//api.weixin.qq.com/cgi-bin/token' params = { "grant_type": "client_credential", "appid": "wx6b11b3efd1cdc290", "secret": "106a9c6157c4db5f6029918738f9529d" } res = requests.get(url, params=params) print(res.text) if __name__ == '__main__': pytest.main(['-vs'])
上面的代码改为yaml格式数据驱动
test_api.yaml
- name: 获得token鉴权码的接口 requests: url: https//api.weixin.qq.com/cgi-bin/token method: get headers: Content-Type: application/json params: grant_type: client_credential appid: wx6b11b3efd1cdc290 secret: 106a9c6157c4db5f6029918738f9529d validate: - eq: {expires_in: 7200}
yaml_util.py
import yaml class YamlUtil: def __init__(self, yaml_file): """ 通过init方法把Yaml文件传入到这个类 :param yaml_file: """ self.yaml_file = yaml_file # 读取Yaml文件 def read_yaml(self): """ 读取Yaml,对yaml反序列化,就是把我们的yaml格式转换成dict格式 :return: """ with open(self.yaml_file, encoding='utf-8')as f: value = yaml.load(f, Loader=yaml.FullLoader) return valueif __name__ == '__main__': YamlUtil('test_api.yaml').read_yaml()
测试用例改为如下
import pytest import requests from test_cases.yaml_util import YamlUtil from config.allpath_parameter import test_case_path class TestApi: @pytest.mark.parametrize('args', YamlUtil(test_case_path+'test_api.yaml').read_yaml()) def test_01_huahua(self, args): print(args) url = args['request']['url'] params = args['request']['params'] res = requests.get(url, params=params) print(res.text) if __name__ == '__main__': pytest.main(['-vs'])
标签:__,self,接口,yaml,params,自动化,test,import From: https://www.cnblogs.com/may18/p/17704555.html