pytest入门六:参数化读取文件json
·
https://zhuanlan.zhihu.com/p/623447031
{
"item":[
{
"request":
{
"url": "xxx",
"body":
{
"password":"123456",
"sex":"男",
"age":12
}
},
"response":
[
{
"message":
{
"username": "用户名不能为空"
}
}
]
},
{
"request":
{
"url": "xxx",
"body":
{
"username": "admin",
"password":"123456",
"sex":"男",
"age":12
}
},
"response":
[
{
"age": 12,
"password": "123456",
"sex": "男",
"username": "admin"
}
]
},
{
"request":
{
"url": "xxx",
"body":
{
"username": "admin",
"sex":"男",
"age":12
}
},
"response":
[
{
"message":
{
"password": "密码不能为空"
}
}
]
},
{
"request":
{
"url": "xxx",
"body":
{
"username": "admin",
"password":"123456",
"sex":"111",
"age":12
}
},
"response":
[
{
"message":
{
"sex": "性别只能是男或者⼥"
}
}
]
},
{
"request":
{
"url": "xxx",
"body":
{
"username": "admin",
"password":"123456",
"sex":"男",
"age":"qqq"
}
},
"response":
[
{
"message":
{
"age": "年龄必须为正整数"
}
}
]
}
]
}
import json
def readJson():
'''
返回Python对象
:return:
'''
return json.load(open("1.json","r",encoding='utf-8'))['item']
import requests
import pytest
@pytest.mark.parametrize("data", readJson())
def test_json_login(data):
'''
1、需要看下返回的数据是否是字典
2、若不是字典还要进行反序列化或序列化的处理
:param data:
:return:
'''
r = requests.post(url=data['request']['url'],json=data['request']['body'])
assert r.json() == data['response'][0]
if __name__ == "__main__":
pytest.main(['-v', '-s','test.py'])
更多推荐


所有评论(0)