Test Overview
This test uses a parametrized fixture in pytest to run the same test with multiple input values. It verifies that the input number is even.
This test uses a parametrized fixture in pytest to run the same test with multiple input values. It verifies that the input number is even.
import pytest @pytest.fixture(params=[2, 4, 6]) def even_number(request): return request.param def test_is_even(even_number): assert even_number % 2 == 0, f"{even_number} is not even"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | pytest collects test_is_even and sees it uses fixture even_number with params [2,4,6] | Test collection complete with 3 test cases generated | — | PASS |
| 3 | pytest runs test_is_even with even_number=2 | Test function receives even_number=2 | assert 2 % 2 == 0 | PASS |
| 4 | pytest runs test_is_even with even_number=4 | Test function receives even_number=4 | assert 4 % 2 == 0 | PASS |
| 5 | pytest runs test_is_even with even_number=6 | Test function receives even_number=6 | assert 6 % 2 == 0 | PASS |
| 6 | All parametrized tests passed | Test report shows 3 passed tests | — | PASS |
@pytest.fixture(params=[...]) in pytest?@pytest.fixture with the keyword params as a list.request and return request.param to access each parameter value.import pytest
@pytest.fixture(params=[10, 20])
def number(request):
return request.param
def test_double(number):
assert number * 2 in [20, 40]@pytest.fixture(params=["a", "b"])
def letter():
return param
def test_letter(letter):
assert letter in ["a", "b"]params, so it must accept request to access request.param.param, which is undefined. It should return request.param.pytest.skip() when the current parameter is invalid to skip that test run.skipif on test function is less flexible for per-parameter skipping.Pass the test and this pattern moves unknown