I am trying to avoid repeating too much boilerplate in my tests, and I want to rewrite them in a more structured way. Let's say that I have two different parsers that both can parse a text into a doc
. That doc would then be used in other tests. The end goal is to expose a doc()
fixture that can be used in other tests, and that is parameterised in such a way that it runs all combinations of given parsers and texts.
@pytest.fixture
def parser_a():
return "parser_a" # actually a parser object
@pytest.fixture
def parser_b():
return "parser_b" # actually a parser object
@pytest.fixture
def short_text():
return "Lorem ipsum"
@pytest.fixture
def long_text():
return "If I only knew how to bake cookies I could make everyone happy."
The question is, now, how to create a doc()
fixture that would look like this:
@pytest.fixture(params=???)
def doc(parser, text):
return parser.parse(text)
where parser
is parameterised to be parser_a and parser_b, and text
to be short_text and long_text. This means that in total doc
would test four combinations of parsers and text in total.
The documentation on PyTest's parameterised fixtures is quite vague and I could not find an answer on how to approach this. All help welcome.