本篇博客比较下 parametrize 中 参数为 True 和 False 时,不同 的表现。
# File : test_demo_11.py # IDE : PyCharm import pytest @pytest.fixture(params=['a', 'b', 'c']) def fixture_and_paramterize(request): print('\n执行参数{}'.format(request.param)) return request.param # indirect=False @pytest.mark.parametrize('fixture_and_paramterize', ['d', 'e', 'f'], indirect=False) def test_fixture_and_paramterize(fixture_and_paramterize): print('indirect=False,参数为{}'.format(fixture_and_paramterize)) # indirect=False @pytest.mark.parametrize('fixture_and_paramterize', ['d', 'e', 'f'], indirect=True) def test_fixture_and_paramterize_2(fixture_and_paramterize): print('indirect=True,参数为{}'.format(fixture_and_paramterize))
执行代码:
当 indirect=True 时,argnames 参数被当成函数执行,且 argvalues 值被当成函数中的参数传参。
当 indirect=True 时,argnames 参数被当成普通变量
E:\personal\python38\python.exe E:/personal/GitWorkSpace/pytest_basic/main.py test_demo_11.py::test_fixture_and_paramterize[d] test_demo_11.py::test_fixture_and_paramterize[e] test_demo_11.py::test_fixture_and_paramterize[f] test_demo_11.py::test_fixture_and_paramterize_2[d] test_demo_11.py::test_fixture_and_paramterize_2[e] test_demo_11.py::test_fixture_and_paramterize_2[f] indirect=False,参数为d .indirect=False,参数为e .indirect=False,参数为f . 执行fixture函数,执行后参数为d indirect=True,参数为d . 执行fixture函数,执行后参数为e indirect=True,参数为e . 执行fixture函数,执行后参数为f indirect=True,参数为f . 6 passed in 0.03s Process finished with exit code 0