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报告集成,并且根据自动化的错误率来发送电子邮件