参考链接:https://www.yuque.com/poloyy/nz6yd2/yt5s5v
import pytest """ 传一个参数: 知识点: 1、test_login的参数化中 indirect=True 是指吧login 当成一个函数去执行,nameList表示login函数所需的参数 2、test_login()方法的login,指的是fixture的返回值 """ nameList=["test2","test1"] @pytest.fixture(scope="function") def login(request): name = request.param print(f"fixture中{name}") return name @pytest.mark.parametrize("login",nameList,indirect=True) #知识点1 def test_login(login): #知识点2 print(f"用例登录的账号是--{login}---") if __name__ == '__main__': pytest.main(['-s', 'test_resuqest.py'])
#传两个参数
@pytest.fixture() def logins(request): param = request.param print(f"fixture输出账号是:{param['username']}") print(f"fixture输出密码是:{param['pwd']}") return param data = [ {"username": "test1", "pwd": "pwd1"}, {"username": "test2", "pwd": "pwd2"}, ] #知识点:如果需要传多个参数,需要通过字典去传 @pytest.mark.parametrize("logins", data, indirect=True) def test_name_pwd(logins): print(f"用例输出账号是:{logins['username']}") print(f"用例输出密码是:{logins['pwd']}")