使用pytest内置的装饰器 @pytest.mark.parametrize()
1.JSON文件驱动
{
"test_data": [
[
{
"alias": "haozi",
"canComment": "False",
"content": "11111",
"editorType": "markdown",
"markdown": "11111",
"privacy": "False",
"recommended": "False",
",rubbish": "False",
",title": "laoshu",
"typeId": ""
},
"None"
],
[
{
"alias": "haozi",
"canComment": "False",
"content": "11111",
"editorType": "markdown",
"markdown": "11111",
"privacy": "False",
",recommended": "False",
",rubbish": "False",
"title": "laoshu",
"typeId": "AAA"
},
"java.sql.SQLException: Incorrect integer value: 'AAA' for column 'typeId' at row 1"
]
]
}
data = json.load(open('D:/tools/pycharm/autotest/data/article.json',mode='r',encoding='utf8')) #使用load加载返回一个字典
test_data = data['test_data']
@pytest.mark.parametrize('article_data,msg',test_data)
def test_create_article(article_data,msg):
r = requests.post(baseurl, json=article_data, headers=heards)
logger.debug(f'发送请求:{r}')
assert str(r.json()['message']) == msg
2.Excel文件驱动
wb = load_workbook("D:/tools/pycharm/autotest/data/data.xlsx")
ws = wb['articles']
test_data = []
for x in range(2,len(tuple(ws.rows))+1): #循环行
testcase_data = []
for y in range(2,7): #循环某一行的所有列
testcase_data.append(ws.cell(row=x, column=y).value) #循环完一列把单元格数据放到列表里,一行数据组成一个列表
print(ws.cell(row=x, column=y).value)
test_data.append(testcase_data) #循环完一行数据把列表放到test_data列表
标签:False,json,pytest,参数,test,article,data From: https://www.cnblogs.com/yitian395/p/16821641.html
##test_data = file_utils.parse_excel_file('D:/tools/pycharm/autotest/data/data.xlsx','articles')
@pytest.mark.parametrize('url,method,article_data,code,msg',test_data)
def test_article_create(url,method,article_data,code,msg):
if method == 'post':
r = requests.post(url,article_data,headers=headers)
logger.debug(f'发送请求:{r}')
assert r.status_code == code
assert r.json()['message'] == msg