pytest2.参数化parametrize

1.pytest参数化使用pytest的装饰器pytest.mark.parametrize

# coding=utf-8

import pytest

@pytest.mark.parametrize('test_input, expected', # 两个参数,参数放到元组中,所有的参数放到列表中
[
('3+4', 7),
('3*4', 12),
('8/2', 4)
])
def test01(test_input, expected): # 传入两个参数
assert eval(test_input) == expected

 2.pytest参数化,通过函数传递

test_login_data2 = [
{"user": "admin1", "psw": "111111"},
{"user": "admin2", "psw": "22222"},
{"user": "admin3", "psw": "333333333"},
{"user": "admin4", "psw": "4444444"}
]


@pytest.fixture(scope="function") # 每个测试函数运行时执行一次,相当于unittest的setUpfunction()
def login2(request):
user = request.param["user"]
psw = request.param["psw"]
print("登录账户:%s" % user)
print("登录密码:%s" % psw)
if psw:
return True
else:
return False
@pytest.mark.parametrize('login2', test_login_data2, indirect=True) # 这里要注意变量名称要与定义的函数名称一致,否则会报错
def test_login2(login2):
a = login2
print("测试用例中login的返回值:%s" % a)
assert a, "失败原因:密码为空"


3.直接使用数据

@pytest.mark.parametrize('datas', test_login_data2)
def test_datas(datas):
print(datas['user'])
print(datas['psw'])

原文地址:https://www.cnblogs.com/leslie003/p/13033441.html