使用pytest.mark.parametrize進(jìn)行參數(shù)化(pytest內(nèi)置的裝飾器)
其中g(shù)et_test_data().getDatasFromSheet())是封裝的讀取excel的測試數(shù)據(jù)(也可以是其他類型的文件存儲測試數(shù)據(jù)隐圾,只要能讀出來就行)
參數(shù)'grade,expect'是將變量放在一個(gè)字符串里,如果是多個(gè)則用逗號隔開
class TestHomeIndex():
@pytest.mark.parametrize('grade,expect', get_test_data().getDatasFromSheet())
def test_001_home_index(self, http, get_data, grade,expect):#前置操作和輸入?yún)?shù)
'''測試首頁不同年級'''
logger.info(f"{grade}-=====-{expect}")
# print("grade",grade,type(grade))
uri = '/mall/app/home/index'
payload = get_data.get('playload')
payload['gradeId'] = str(grade)
#print(payload,type(payload))
headers = get_data.get('headers')
headers['X-Grade'] = str(grade)
#print(headers,type(headers))
logger.info(headers)
logger.info(payload)
response = http.post(uri, data=payload, headers=headers)
# print('用例中的url',response.url)
logger.info(response.text)
r = response.json()
print(json.dumps(r, indent=4, ensure_ascii=False))
# print(response.status_code)
code = response.json()['code']
# print(code)
# print(response.json()['data']['public']['user_type'])
assert response.status_code == 200, '請求返回非200'
assert code == expect, '請求返回code非0'
利用fixture實(shí)現(xiàn)參數(shù)化,多個(gè)參數(shù)校焦,會(huì)生成各參數(shù)笛卡爾積的測試用例孙乖,params是列表類型
request參數(shù)固定寫法莉恼,通過request.param返回
user_name = ["admin","staff"]
user_pwd = ["123","456"]
@pytest.fixture(params=user_name)
def login_name(request):
name = request.param
return name
@pytest.fixture(params=user_pwd)
def login_pwd(request):
pwd = request.param
return pwd
def test_login(login_name,login_pwd):
print(login_name,login_pwd)
assert login_name == "admin"
assert login_pwd == "123"