danijar
danijar

Reputation: 34215

How to use override parameters of a fixture in Pytest?

Let's say I have a parameterized fixture like this:

@pytest.fixture(params=[1, 2, 800]):
def resource(request):
    return Resource(capacity=request.param)

When I use the fixture as parameter in a test function, Pytest runs the test with all three versions:

def test_resource(resource):  # Runs for capacities 1, 2, and 800.
    assert resource.is_okay()

However, for some tests I want to change the parameters for which the fixture gets built:

def test_worker(resource, worker):  # Please run this for capacities 1 and 5.
    worker.use(resource)
    assert worker.is_okay()

How can I specify to only receive certain versions of the specified fixture?

Upvotes: 5

Views: 3771

Answers (2)

AlokThakur
AlokThakur

Reputation: 3741

If you want to use different set of parameters for different tests then pytest.mark.parametrize is helpful.

@pytest.mark.parametrize("resource", [1, 2, 800], indirect=True)
def test_resource(resource):
    assert resource.is_okay()

@pytest.mark.parametrize("resource", [1, 5], indirect=True)
def test_resource_other(resource):
    assert resource.is_okay()

Upvotes: 6

jonrsharpe
jonrsharpe

Reputation: 122089

I don't think that you can configure it "to only receive certain versions", but you can explicitly ignore some of them:

def test_worker(resource, worker):
    if resource.capacity == 800:
        pytest.skip("reason why that value won't work")
    worker.use(resource)
    assert worker.is_okay()

Upvotes: 3

Related Questions