Reputation: 19057
From the pytest documentation:
@pytest.mark.parametrize allows one to define multiple sets of arguments and fixtures at the test function or class.
It looks like this means that pytest.mark.parametrize
can mark a test to run with multiple sets of fixtures? I can find plenty examples of parametrizing arguments, but I can't figure out how to parametrize different sets of fixtures.
I think this answer comes close, but that's really just parametrizing arguments, then resolving the different fixtures in the test body.
Is it possible to mark a test to run multiple times, using different sets of fixtures?
Note I'm trying to do something like this:
import pytest
# some data fixutres
@pytest.fixture()
def data1():
"""Create some data"""
@pytest.fixture()
def data2():
"""Create some different data"""
@pytest.fixture()
def data3():
"""Create yet different data"""
# The tests
@pytest.mark.parametrize('data', [data1, data2])
def test_foo(data):
"""Test something that makes sense with datasets 1 and 2"""
@pytest.mark.parametrize('data', [data2, data3])
def test_bar(data):
"""Test something that makes sense with datasets 2 and 3"""
Upvotes: 7
Views: 2286
Reputation: 7048
You can do this with the pytest-lazy-fixture plugin:
import pytest
from pytest_lazyfixture import lazy_fixture
@pytest.fixture()
def fixture1():
return "data1"
@pytest.fixture()
def fixture2():
return "data2"
@pytest.fixture()
def fixture3():
return "data3"
@pytest.mark.parametrize("data", [lazy_fixture("fixture1"),
lazy_fixture("fixture2")])
def test_foo(data):
assert data in ("data1", "data2")
@pytest.mark.parametrize("data", [lazy_fixture("fixture2"),
lazy_fixture("fixture3")])
def test_bar(data):
assert data in ("data2", "data3")
Note that there's a proposal to add similar functionality directly to pytest: https://docs.pytest.org/en/latest/proposals/parametrize_with_fixtures.html
Upvotes: 4