@pytest.mark.parametrize

  1. @pytest.mark.parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None)):
参数 说明
argnames 必传,参数名, 以逗号分隔的字符串,表示一个或多个参数名称(key),或参数字符串的列表/元组
argvalues 必传,参数值,若argnames有一个刚单值列表传入,若argnames有多个,以套用元组的列表展示,无组内与参数名一一对应
scope 如果指定,则表示参数的范围。范围用于按参数实例对测试进行分组。它还将覆盖任何fixture函数定义的范围,允许使用测试上下文或配置设置动态范围
ids 标记子用例执行名称,与argvalues数量一致,未指定自动生成,默认None,增加可读性,中文需要转码
indirect 为true时,fixture函数名称将作为argnamesf的参数值,相当于@pytest.fixture(params=)的用法,默认False
  1. num=[1,2]
  2. @pytest.fixture()
  3. def testA():
  4. pass
  5. @pytest.mark.parametrize('testA', num, indirect=True)
  6. def test(testB):
  7. pass
  1. num=[1,2]
  2. @pytest.fixture(params=num)
  3. def testA():
  4. pass
  5. @pytest.mark.parametrize('testA', num)
  6. def test(testB):
  7. pass

单参单值
  1. import pytest
  2. @pytest.mark.parametrize('value', [2])
  3. def test_argnames(value):
  4. """
  5. :param value: 将argnames做为形参传入,传入单个参数
  6. :return:
  7. """
  8. print(f"传入的值为:{value}")
  9. assert value == 2
  10. if __name__ == '__main__':
  11. pytest.main(['-vs', '--tb=line', 'test.py'])
  12. # test.py::test_argnames[2]
  13. # 传入的值为:2
  14. # PASSED
  15. # ============================== 1 passed in 0.03s ==============================

单参多值
  1. import pytest
  2. @pytest.mark.parametrize('arg', ['1+2', 1 + 2, '1' + '2', {'a': 1, 'b': 2}, (1, 2)])
  3. def test_one_params(arg):
  4. '''
  5. :param arg: 将argnames做为形参传入,传入多个参数
  6. :return:
  7. '''
  8. print("传入的值为:{}".format(arg))
  9. assert arg == 3
  10. if __name__ == '__main__':
  11. pytest.main(['-vs', '--tb=line', 'test.py'])
  12. # test.py::test_one_params[1+2] 传入的值为:1+2
  13. # FAILED
  14. # test.py::test_one_params[3] 传入的值为:3
  15. # PASSED
  16. # test.py::test_one_params[12] 传入的值为:12
  17. # FAILED
  18. # test.py::test_one_params[arg3] 传入的值为:{'a': 1, 'b': 2}
  19. # FAILED
  20. # test.py::test_one_params[arg4] 传入的值为:(1, 2)
  21. # FAILED
  22. # ================================== FAILURES ===================================
  23. # D:\pycharm\api\test.py:10: AssertionError: assert '1+2' == 3
  24. # D:\pycharm\api\test.py:10: AssertionError: assert '12' == 3
  25. # D:\pycharm\api\test.py:10: AssertionError: assert {'a': 1, 'b': 2} == 3
  26. # D:\pycharm\api\test.py:10: assert (1, 2) == 3
  27. # =========================== short test summary info ===========================
  28. # FAILED test.py::test_one_params[1+2] - AssertionError: assert '1+2' == 3
  29. # FAILED test.py::test_one_params[12] - AssertionError: assert '12' == 3
  30. # FAILED test.py::test_one_params[arg3] - AssertionError: assert {'a': 1, 'b': ...
  31. # FAILED test.py::test_one_params[arg4] - assert (1, 2) == 3
  32. # ========================= 4 failed, 1 passed in 0.06s =========================

多参多值
  1. import pytest
  2. @pytest.mark.parametrize("input_num,expected", [("2**2", 4), ("5-2", 1), ("5*2", 10)])
  3. def test_params(input_num, expected):
  4. '''
  5. :param input_num: 计算的数值
  6. :param expected: 预期结果
  7. :return:
  8. '''
  9. print(f"原值:{input_num} 期望值{expected}")
  10. # eval 去引号
  11. assert eval(input_num) == expected
  12. # test.py::test_params[2**2-4] 原值:2**2 期望值4
  13. # PASSED
  14. # test.py::test_params[5-2-1] 原值:5-2 期望值1
  15. # FAILED
  16. # test.py::test_params[5*2-10] 原值:5*2 期望值10
  17. # PASSED
  18. # ================================== FAILURES ===================================
  19. # D:\pycharm\api\test.py:13: assert 3 == 1
  20. # =========================== short test summary info ===========================
  21. # FAILED test.py::test_params[5-2-1] - assert 3 == 1
  22. # ========================= 1 failed, 2 passed in 0.05s =========================

@pytest.fixture(scope,autouse)

单fixture单值
  1. import pytest
  2. test_data = ["admin", "root"]
  3. @pytest.fixture()
  4. def register_users(request):
  5. # pytest固定写法,用于接收传入的值并且调用
  6. username = request.param
  7. # 返回结果,为了让被测函数调用
  8. return username
  9. # 将fixture对应的函数作为parameter的argnames参数传入,相当于调用了fixture对应的函数
  10. # 将测试数据test_data作为argvalues的值传入
  11. # 省去了fixture的传参,pytest将值传入,并调用
  12. @pytest.mark.parametrize('register_users', test_data, indirect=True)
  13. def test_register(register_users):
  14. # format(register_users)中为调用的fixture的与parametrize无关
  15. print('用户输入的内容为:{}'.format(register_users))
  16. if __name__ == '__main__':
  17. pytest.main(['-vs', '--tb=line', 'test.py'])
  18. # # test.py::test_register[user1] 用户输入的内容为:user1
  19. # PASSED
  20. # test.py::test_register[user2] 用户输入的内容为:user2
  21. # PASSED
  22. # ============== 2 passed in 0.04s ==============================

单fixture多值
  1. import pytest
  2. import pytest
  3. import pytest
  4. userinfo = [
  5. {'user':'张三','pwd':123},
  6. {'user':'李四','pwd':456}
  7. ]
  8. @pytest.fixture()
  9. def login(request):
  10. user = request.param
  11. print("传入的用户名为:{},密码为:{}".format(user['user'],user['pwd']))
  12. return user
  13. @pytest.mark.parametrize('login',userinfo,indirect=True)
  14. def test_one_param(login):
  15. print("测试类的读到的用户是:{} 密码是:{}".format(login['user'],login['pwd']))
  16. if __name__ == '__main__':
  17. pytest.main(['-vs', '--tb=line', 'test.py'])
  18. # test.py::test_one_param[login0] 传入的用户名为:张三,密码为:123
  19. # 测试类的读到的用户是:张三 密码是:123
  20. # PASSED
  21. # test.py::test_one_param[login1] 传入的用户名为:李四,密码为:456
  22. # 测试类的读到的用户是:李四 密码是:456
  23. # PASSED
  24. # ============================== 2 passed in 0.03s ==============================

多fixture多值
  1. import pytest
  2. #一个@pytest.mark.parametrize使用多个fixture,传入的数据要是嵌套了元组的列表
  3. userinfo = [
  4. ('张三',123),
  5. ('李四','pwd')
  6. ]
  7. @pytest.fixture()
  8. def login_user(request):
  9. user = request.param
  10. print("传入的用户名为:{}".format(user))
  11. return user
  12. @pytest.fixture()
  13. def login_pwd(request):
  14. pwd = request.param
  15. print("密码为:{}".format(pwd))
  16. return pwd
  17. @pytest.mark.parametrize('login_user,login_pwd',userinfo,indirect=True)
  18. def test_one_param(login_user,login_pwd):
  19. print("测试类的读到的用户是:{} 密码是:{}".format(login_user,login_pwd))

叠加fixture
  1. import pytest
  2. user = ['张三', '李四']
  3. pwd = [124, 345]
  4. @pytest.fixture()
  5. def login_user(request):
  6. user = request.param
  7. print("传入的用户名为:{}".format(user))
  8. return user
  9. @pytest.fixture()
  10. def login_pwd(request):
  11. pwd = request.param
  12. print("密码为:{}".format(pwd))
  13. return pwd
  14. @pytest.mark.parametrize('login_pwd', pwd, indirect=True)
  15. @pytest.mark.parametrize('login_user', user, indirect=True)
  16. def test_one_param(login_user, login_pwd):
  17. print("测试类的读到的用户是:{} 密码是:{}".format(login_user, login_pwd))
  18. if __name__ == '__main__':
  19. pytest.main(['-vs', '--tb=line', 'test.py'])
  20. # test.py::test_one_param[\u5f20\u4e09-124] 传入的用户名为:张三
  21. # 密码为:124
  22. # 测试类的读到的用户是:张三 密码是:124
  23. # PASSED
  24. # test.py::test_one_param[\u5f20\u4e09-345] 传入的用户名为:张三
  25. # 密码为:345
  26. # 测试类的读到的用户是:张三 密码是:345
  27. # PASSED
  28. # test.py::test_one_param[\u674e\u56db-124] 传入的用户名为:李四
  29. # 密码为:124
  30. # 测试类的读到的用户是:李四 密码是:124
  31. # PASSED
  32. # test.py::test_one_param[\u674e\u56db-345] 传入的用户名为:李四
  33. # 密码为:345
  34. # 测试类的读到的用户是:李四 密码是:345
  35. # PASSED
  36. # ============================== 4 passed in 0.03s ==============================