pytest参数化测试!
参数化测试允许传递多组数据,一旦发现测试失败,pytest会及时报告。
@pytest.mark.parametrize(argnames,argvalues)装饰器可以达到批量传送参数的目的
第一步:用python的requests请求一个接口:
import requests
import pytest
import allure
class TestParam1(object):
"""
"""
@pytest.fixture(scope="class", autouse=True)
def prepare(self, request):
with allure.step("测试数据准备:"):
pass
@allure.step("测试数据数据清理:")
def fin():
"""
Clean up test environment after testing
"""
pass
request.addfinalizer(fin)
def test_param_1(self):
url = "http://127.0.0.1:8000/add_message/"
headers = {
'Content-Type': "application/x-www-form-urlencoded",
}
payload ={
"mid" :"106",
"name" :"android4",
"content" : "8" ,
"status": "1",
"author" :"xixi"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.json())
assert response.json()["message"] == "add message success"
第二步:使用装饰器
#!/usr/bin/python
# coding=utf-8
import pytest
import requests
from utils.get_data import get_data_path
from utils.get_data import get_test_data
param=[
({'Content-Type': 'application/x-www-form-urlencoded'}, {'name': 'android11', 'content': '8', 'status': '1', 'author': 'xixi', 'mid': '110'}),
({'Content-Type': 'application/x-www-form-urlencoded'}, {'name': 'android12', 'content': '8', 'status': '1', 'author': 'xixi', 'mid': '111'}),
({'Content-Type': 'application/x-www-form-urlencoded'}, {'name': 'android13', 'content': 'hahah', 'status': '1', 'author': 'xixi', 'mid': '112'})
]
class TestParam2(object):
"""
"""
@pytest.fixture(scope="class", autouse=True)
def prepare(self, request):
pass
def fin():
"""
Clean up test environment after testing
"""
pass
request.addfinalizer(fin)
@pytest.mark.parametrize("headers,payload", param)
def test_param_2(self,headers,payload):
url = "http://127.0.0.1:8000/add_message/"
response = requests.request("POST", url, data=payload, headers=headers)
res = response.json()
#assert res["message"] == "add message success"
第三步:编写获取测试data的函数
def get_data_path(case_path):
file_name = os.path.dirname(case_path).split(os.sep + 'tests' + os.sep, 1)
test_data = os.sep.join([file_name[0], 'data', file_name[1], os.path.basename(case_path).replace('.py', '.json')])
return test_data
def get_test_data(test_data_path):
case = []
headers = []
querystring = []
payload = []
expected = []
with open(test_data_path,encoding='utf-8') as f:
dat = json.loads(f.read())
test = dat['test']
for td in test:
case.append(td['case'])
headers.append(td.get('headers', {}))
querystring.append(td.get('querystring', {}))
payload.append(td.get('payload', {}))
expected.append(td.get('expected', {}))
list_parameters = list(zip(case, headers, querystring, payload, expected))
return case, list_parameters
第四步:将测试data与case相分离
#!/usr/bin/python
# coding=utf-8
import pytest
import requests
from utils.get_data import get_data_path
from utils.get_data import get_test_data
case,param = get_test_data(get_data_path(__file__))
class TestParam2(object):
"""
"""
@pytest.fixture(scope="class", autouse=True)
def prepare(self, request):
pass
def fin():
"""
Clean up test environment after testing
"""
pass
request.addfinalizer(fin)
@pytest.mark.parametrize("case,headers,querystring,payload,expected", param,ids=case)
def test_param_2(self,case,headers,querystring,payload,expected):
url = "http://127.0.0.1:8000/add_message/"
response = requests.request("POST", url, data=payload, headers=headers)
res = response.json()
assert res["message"] == "add message success"
第五部:test 大功告成~!