renynim
renynim

Reputation: 11

How can I test for specific values only with pytest?

I would like to know how to test only certain values through the pytest. The code I want to perform is as follows.

import pytest

a = [1, 2]
b = [1, 2, 3]


@pytest.fixture(params=a)
def return_a(request):
    return request.param


@pytest.fixture(params=b)
def return_b(request):
    return request.param


def test(return_a, return_b):
    if return_a == 1:
        assert return_a <= return_b

In this case, the output values are as follows:

sample.py::test[1-1] PASSED                                                [ 16%] 
sample.py::test[1-2] PASSED                                                [ 33%] 
sample.py::test[1-3] PASSED                                                [ 50%] 
sample.py::test[2-1] PASSED                                                [ 66%] 
sample.py::test[2-2] PASSED                                                [ 83%]
sample.py::test[2-3] PASSED                                                [100%] 

================================6 passed in 0.09s================================

However, the result that I want is that the test is conducted only when a is 1.

Do you have any good ideas?

Upvotes: 1

Views: 320

Answers (1)

hoefling
hoefling

Reputation: 66431

You will have to reparameterize the particular tests to overwrite the default parametrization of the return_a fixture. There's no way of declaring some kind of filter per test. Example with indirect parametrization:

# use the default parametization in fixtures
def test_all(return_a, return_b):
    ...

# overwrite parametrization of `return_a`
@pytest.mark.parametrize("return_a", [1], indirect=True)
def test_some(return_a, return_b):
    ...

Upvotes: 1

Related Questions