本文主要介绍:将接口返回的字典值{"x1":1,"x2":2....}手动存到txt,然后写代码断言每个value值是否都满足某个区间。运用到:
1、读取txt文件中的数据
2、pytest写用例格式
3、pytest.assume断言方式(出现报错AttributeError: module ‘pytest‘ has no attribute ‘assume‘时,运行方式改成pytest.main(['-vs', 'dataVali.py'])就可以了)
1、新建txt文件存放数据,front_data.txt
{"414681000":null,"759209194180041205":11636.968302652036,"759209064180036770":10554.777317943637,"759209150180039736":10709.94209512658,"759208980180032095":10242.952290503805}
2、新建dataVali.py文件,断言每个value的值
# 读取txt中的数据转成字典
import pytest
def openreadtxt(file_name):
data = ''
file = open(file_name, 'r') # 打开文件
file_data = file.readlines() # 读取所有行
for row in file_data:
row = row.replace('\n', '') # 去掉换行符
row = row.replace('null', 'None') #
print(row)
data += row
data = eval(data)
return data
# 获取字典的值,判断是否正确
def test_get_data():
data = openreadtxt('front_data.txt')
dict_data = data
print(dict_data)
# actual = []
for key, value in dict_data.items():
if value is not None:
pytest.assume(11000 <= value <= 12000, f'{key}的值是{value},不在区间内')
else:
print(f'{key}的值是None,没有参与比较')
pass
if __name__ == "__main__":
pytest.main(['-vs', 'dataVali.py'])
3、运行结果
控制台打印断言失败的数据