Test Overview
This test uses two @pytest.mark.parametrize decorators to run a function with all combinations of two sets of inputs. It verifies the sum of two numbers equals the expected result.
This test uses two @pytest.mark.parametrize decorators to run a function with all combinations of two sets of inputs. It verifies the sum of two numbers equals the expected result.
import pytest @pytest.mark.parametrize('a', [1, 2]) @pytest.mark.parametrize('b', [10, 20]) def test_sum(a, b): result = a + b assert result == a + b # simple check to confirm sum calculation
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | pytest collects test_sum with parameters a=[1,2] and b=[10,20] | Test cases generated for all combinations: (a=1,b=10), (a=1,b=20), (a=2,b=10), (a=2,b=20) | — | PASS |
| 3 | Test case runs with a=1, b=10 | Function test_sum called with a=1, b=10 | Assert result == 11 | PASS |
| 4 | Test case runs with a=1, b=20 | Function test_sum called with a=1, b=20 | Assert result == 21 | PASS |
| 5 | Test case runs with a=2, b=10 | Function test_sum called with a=2, b=10 | Assert result == 12 | PASS |
| 6 | Test case runs with a=2, b=20 | Function test_sum called with a=2, b=20 | Assert result == 22 | PASS |
| 7 | All test cases completed | All combinations tested successfully | — | PASS |
@pytest.mark.parametrize decorators on a single test function?@pytest.mark.parametrize decorator adds a set of parameters to the test function.@pytest.mark.parametrize decorators for parameters x and y?@pytest.mark.parametrize decorators, one for each parameter. -> Option D@pytest.mark.parametrize('a', [1, 2])
@pytest.mark.parametrize('b', [3, 4, 5])
def test_sum(a, b):
assert a + b > 0
@pytest.mark.parametrize('x', [1, 2])
@pytest.mark.parametrize('y', 3, 4)
def test_func(x, y):
assert x != y
@pytest.mark.parametrize('y', 3, 4), which is invalid because values must be in a list or iterable.[3, 4]. Without brackets, it causes a syntax or runtime error.color and size. Colors are ['red', 'blue'], sizes are ['S', 'M', 'L']. You want to skip tests where color is 'red' and size is 'L'. How can you combine multiple @pytest.mark.parametrize decorators and skip that case?pytest.skip() to skip that test case.Pass the test and this pattern moves unknown