1 import pytest 2 source:https://www.cnblogs.com/yoyoketang/p/9492132.html 3 4 # 测试账号数据 5 test_user_data = [{"user": "admin1", "psw": "111111"}, 6 {"user": "admin1", "psw": ""}] 7 8 @pytest.fixture(scope="module") 9 def login(request): 10 user = request.param["user"] 11 psw = request.param["psw"] 12 print("登录账户:%s" % user) 13 print("登录密码:%s" % psw) 14 if psw: 15 return True 16 else: 17 return False 18 19 # indirect=True 声明login是个函数 20 @pytest.mark.parametrize("login", test_user_data, indirect=True) 21 def test_login(login): 22 '''登录用例''' 23 a = login 24 print("测试用例中login的返回值:%s" % a) 25 assert a, "失败原因:密码为空" 26 27 if __name__ == '__main__': 28 print('start') 29 pytest.main(["-s", "-v", "testfixture.py"]) 30 print('end')
如果把request参数替换成其他(如 myparam), 会报错;
替换代码:
1 @pytest.fixture(scope="module") 2 def login(myparam): 3 user = myparam.param["user"] 4 psw = myparam.param["psw"] 5 print("登录账户:%s" % user) 6 print("登录密码:%s" % psw) 7 if psw: 8 return True 9 else: 10 return False
E fixture 'myparam' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, login, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.