您的夹具应如下所示:
@pytest.fixture(scope='function')
def doc_fixture(request):
parser = request.param[0]
text = request.param[1]
return parser.parse(text)
并以下列方式使用它:
@pytest.mark.parametrize('doc_fixture', [parser_1, 'short text'], indirect=True)
def test_sth(doc_fixture):
... # Perform tests
您可以使用混合和匹配参数组合pytest.mark.parametrize
这是另一个提供不同参数组合的示例:
from argparse import Namespace
import pytest
@pytest.fixture(scope='function')
def doc_fixture(request):
first_arg, second_arg = request.param
s = Namespace()
s.one = first_arg
s.two = second_arg
return s
@pytest.mark.parametrize(
'doc_fixture',
[
('parserA', 'ShortText'),
('parserA', 'LongText'),
('parserB', 'ShortText'),
('parserB', 'LongText')
],
indirect=True
)
def test_something(doc_fixture):
assert doc_fixture == ''
以及一个示例运行结果(如预期的那样测试失败):
=========================================================================================== short test summary info ============================================================================================
FAILED ../../tmp/::test_something[doc_fixture0] - AssertionError: assert Namespace(one='parserA', two='ShortText') == ''
FAILED ../../tmp/::test_something[doc_fixture1] - AssertionError: assert Namespace(one='parserA', two='LongText') == ''
FAILED ../../tmp/::test_something[doc_fixture2] - AssertionError: assert Namespace(one='parserB', two='ShortText') == ''
FAILED ../../tmp/::test_something[doc_fixture3] - AssertionError: assert Namespace(one='parserB', two='LongText') == ''