目录结构:
?yam_util.py
import yaml
"""
问题1:解析的yaml文件必须是utf-8的格式,可以用notepad++来查看格式
"""
def read_yaml():
"""
读取yaml文件
:return:
"""
with open('get_new.yaml', encoding='UTF-8')as f:
data = yaml.load(f, Loader=yaml.FullLoader)
return data
if __name__ == '__main__':
print(read_yaml())
?get_new.yaml
-
api_name: 获取网易新闻
api_request:
url: https://api.apiopen.top/getWangYiNews
method: post
headers:
Content-Type: application/json
params:
page: 1
count: 5
api_validate:
- eq: {code: 200}
test_api.py
import pytest
from yaml_util import read_yaml
import requests
class TestApi:
# 最基本的用法
# @pytest.mark.parametrize("args", ['百里', '星瑶', '依然', '修习人生'])
# def test_01_api(self, args):
# """
# 获得网易新闻的接口
# :return:
# """
# print(args)
# 解包的用法(ddt,unittest这个框架实现数据驱动的装饰器,@unpack)
@pytest.mark.parametrize("args", read_yaml())
def test_01_api(self, args):
"""
获得网易新闻的接口
:return:
"""
url = args['api_request']['url']
method = args['api_request']['method']
headers = args['api_request']['headers']
params = args['api_request']['params']
validate = args['api_validate']
print(validate)
if method == 'get':
requests.get()
else:
res = requests.post(url, json=params, )
for val in validate:
assert val['eq']['code'] == res.json()['code']
# print(url)
if __name__ == '__main__':
pytest.main(['-vs', 'test_api.py'])
|