当前位置:   article > 正文

详解pytest传递参数的几种方式_pytest 传参

pytest 传参

测试类内部,属性传递

  1. import pytest
  2. class Test_Case:
  3. t = 0
  4. def test_c(self):
  5. self.t = self.t + 1
  6. assert self.t == 1
  7. def test_d(self):
  8. self.t = self.t + 1
  9. assert self.t == 1
  10. # t是测试类的属性,可以为所有测试方法共享该值,该值是固定不变的

global方式传递

  1. import pytest
  2. s = {}
  3. class Test_Case:
  4. def test_b(self):
  5. global s
  6. s['name'] = 'hello'
  7. print(s['name'])
  8. assert s['name'] == 'hello'
  9. def test_c(self):
  10. global s
  11. s['age'] = 18
  12. print(s)
  13. assert s['age'] == 18
  14. # global声明的变量可以在整个测试类中共享,值是可变的,global可以去掉,效果相同
  15. @pytest.mark.parametrize()
  1. import pytest
  2. class Test_Case:
  3. @pytest.mark.parametrize("x", [1, 2, 3, 4]) # 传递单个值
  4. def test_b(self, x):
  5. assert x != 5
  6. @pytest.mark.parametrize("x,y", [(1, 2), (3, 4), (2, 3), (4, 6)]) # 多参数,传递元组
  7. def test_c(self, x, y):
  8. print(x + y)
  9. assert x + y != 5
  10. @pytest.mark.parametrize("x,y", [{1, 2}, {3, 4}, {2, 3}, {4, 6}]) # 多参数传递集合
  11. def test_d(self, x, y):
  12. print(x + y)
  13. assert x + y != 6
  14. @pytest.mark.parametrize("x", [{"a": 1, "b": 2}, {"a": 1, "c": 4}]) # 传递字典
  15. def test_e(self, x):
  16. print(x)
  17. assert x["a"] == 1
  18. @pytest.mark.parametrize("x,y", [({"a": 1, "b": 2}, {"a": 3, "c": 4})]) # 多参数传递字典
  19. def test_f(self, x, y):
  20. assert x["a"] == 1
  21. @pytest.mark.parametrize("x", [{"a": 1, "b": 2}]) # 装饰器叠加,传递多参数
  22. @pytest.mark.parametrize("y", [{"a": 1, "b": 2}])
  23. def test_g(self, x, y):
  24. assert y["a"] == 1
  25. @pytest.mark.parametrize(
  26. "test_input,expected",
  27. [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.xfail)],
  28. ) # xfail标记
  29. def test_h(self, test_input, expected):
  30. assert eval(test_input) == expected
  31. @pytest.mark.parametrize(
  32. "test_input,expected",
  33. [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.skip)],
  34. ) # skip标记
  35. def test_i(self, test_input, expected):
  36. assert eval(test_input) == expected

fixtrue传递

  1. import pytest
  2. class Test_Case:
  3. @pytest.fixture
  4. def get_d(self): # 通过fixture值传递
  5. return [1, 2, 3]
  6. def test_a(self, get_d):
  7. x = get_d[0]
  8. assert x == 1
  1. import pytest
  2. class Test_Case:
  3. # params = 'hello'等同于params = ['h','e','l','l','o']
  4. @pytest.fixture(params='hello')
  5. def get_c(self, request):
  6. print(request.param)
  7. return request.param
  8. def test_c(self, get_c):
  9. name = get_c
  10. assert name == 'h'
  11. @pytest.fixture(params=[1, 2], ids=['hello', 'name']) # 可以通过pytest -k <ids>执行指定的用例
  12. def get_d(self, request):
  13. return request.param
  14. def test_d(self, get_d):
  15. name = get_d
  16. assert name == 2
  17. @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
  18. def data_set(self, request):
  19. return request.param
  20. def test_f(self):
  21. pass
  1. import pytest
  2. #fixture嵌套传递
  3. class Test_Case:
  4. @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
  5. def data_set(self, request):
  6. return request.param
  7. @pytest.fixture()
  8. def data_s(self, data_set):
  9. print(data_set)
  10. return data_set
  11. def test_g(self, data_s):
  12. assert data_s == 1
  1. # yield传递
  2. import pytest
  3. class Test_Case:
  4. @pytest.fixture
  5. def s(self):
  6. c = 'test'
  7. yield c
  8. def test_name(self, s):
  9. assert s == "test"

配置文件传递

  1. # test_case.py
  2. import pytest
  3. import _case.constant as d
  4. class Test_Case:
  5. def test_g(self):
  6. d.data = 2
  7. assert d.data == 2
  8. def test_h(self):
  9. assert d.data == 2
  10. # _case.constant.py
  11. data = 1
  12. # 和global使用类似,多个测试文件共享值,但是多个文件共享该值时,会收到测试文件的执行顺序影响
  13. # global只能在一个测试文件中共享值

conftest.py

  1. # conftest.py最好是在项目根目录或者测试文件所在目录
  2. import pytest
  3. @pytest.fixture(scope='session')
  4. def say():
  5. return 'hello'
  6. # test_case.py
  7. import pytest
  8. class Test_Case:
  9. def test_g(self, say):
  10. assert say == 'hello'

命令行参数传参

  1. # conftest.py 全局变量使用
  2. import pytest
  3. def pytest_addoption(parser):
  4. parser.addoption("--file", default="test")
  5. @pytest.fixture
  6. def file_name(request):
  7. return request.config.getoption("--file")
  8. # test_case.py
  9. import pytest
  10. class Test_Case:
  11. def test_name(self, file_name):
  12. assert file_name == "test"
  13. # test_case.py或者直接在测试文件中通过pytestconfig获取,示例如下
  14. def test_name(self, pytestconfig):
  15. print(pytestconfig.getoption('file'))
  16. assert pytestconfig.getoption("file") == "test"

钩子函数传参

  1. # conftest.py
  2. import pytest
  3. def pytest_addoption(parser):
  4. parser.addoption("--file", default="test")
  5. def pytest_generate_tests(metafunc):
  6. file = metafunc.config.getoption('--file')
  7. metafunc.parametrize("case_data", [file])
  8. # test_case.py
  9. import pytest
  10. class Test_Case:
  11. def test_g(self, case_data):
  12. assert case_data == 'test'

复制

  1. # conftest.py
  2. import pytest
  3. def pytest_addoption(parser):
  4. parser.addoption("--file", default="test")
  5. def pytest_generate_tests(metafunc):
  6. file = metafunc.config.getoption('--file')
  7. metafunc.parametrize("case_data", [file])
  8. # test_case.py
  9. import pytest
  10. class Test_Case:
  11. def test_g(self, case_data):
  12. assert case_data == 'test'

到此这篇关于详解pytest传递参数的几种方式的文章就介绍到这了,更多相关pytest传递参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持vb.net教程C#教程python教程

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/739854
推荐阅读
相关标签
  

闽ICP备14008679号