Test Overview
This test uses pytest's parametrize decorator with a condition to run different inputs. It verifies the function returns expected results only for allowed inputs.
This test uses pytest's parametrize decorator with a condition to run different inputs. It verifies the function returns expected results only for allowed inputs.
import pytest def is_even(n): return n % 2 == 0 # Parametrize only even numbers @pytest.mark.parametrize('num', [n for n in [2, 3, 4, 5] if n % 2 == 0]) def test_is_even(num): assert is_even(num) is True
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | pytest collects test_is_even with parametrize values filtered to even numbers [2, 4] | Test cases ready for num=2 and num=4 | — | PASS |
| 3 | Test runs test_is_even with num=2 | Function is_even called with 2 | assert is_even(2) is True | PASS |
| 4 | Test runs test_is_even with num=4 | Function is_even called with 4 | assert is_even(4) is True | PASS |
| 5 | Test ends with all parameterized cases passed | Test report shows 2 passed tests | — | PASS |
pytest.param with skipif inside @pytest.mark.parametrize?pytest.param with skipifpytest.mark.parametrize?import sys
import pytest
@pytest.mark.parametrize('num', [
pytest.param(1, marks=pytest.mark.skipif(sys.version_info < (3,8), reason='Skip on old Python')),
2,
3
])
def test_numbers(num):
assert num > 0
import pytest
@pytest.mark.parametrize('val', [
pytest.param(10, marks=pytest.mark.skipif('sys.version_info < (3,8)', reason='Old Python')),
20
])
def test_val(val):
assert val > 0
mode set to 'fast', 'slow', and 'medium'. However, you want to skip the 'slow' mode test only if an environment variable SKIP_SLOW is set to '1'. Which is the correct way to write this conditional parametrize in pytest?Pass the test and this pattern moves unknown