Test Overview
This test uses pytest to run the same test function with different inputs. It uses ids to give each test case a clear name. The test checks if the input number is even.
This test uses pytest to run the same test function with different inputs. It uses ids to give each test case a clear name. The test checks if the input number is even.
import pytest @pytest.mark.parametrize( "number,expected", [ (2, True), (3, False), (10, True), (7, False) ], ids=["two", "three", "ten", "seven"] ) def test_is_even(number, expected): assert (number % 2 == 0) == expected
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts with pytest runner | pytest ready to run tests | — | PASS |
| 2 | pytest collects test_is_even with 4 parameter sets and IDs | Test cases named: test_is_even[two], test_is_even[three], test_is_even[ten], test_is_even[seven] | — | PASS |
| 3 | pytest runs test_is_even with number=2, expected=True | Running test_is_even[two] | Check if 2 % 2 == 0 equals True | PASS |
| 4 | pytest runs test_is_even with number=3, expected=False | Running test_is_even[three] | Check if 3 % 2 == 0 equals False | PASS |
| 5 | pytest runs test_is_even with number=10, expected=True | Running test_is_even[ten] | Check if 10 % 2 == 0 equals True | PASS |
| 6 | pytest runs test_is_even with number=7, expected=False | Running test_is_even[seven] | Check if 7 % 2 == 0 equals False | PASS |
| 7 | All parameterized tests complete | All tests passed with clear IDs | — | PASS |
ids in @pytest.mark.parametrize?ids in parametrizeids parameter assigns names to each test case, making it easier to identify which input caused a failure.ids.ids = labels [OK]ids, not id, names, or labels.@pytest.mark.parametrize with the argument names, list of tuples, and ids as a list of strings.ids parameter correctly [OK]@pytest.mark.parametrize('num', [10, 20], ids=['ten', 'twenty'])
def test_value(num):
assert num > 5
ids list assigns 'ten' to the first input (10) and 'twenty' to the second input (20).@pytest.mark.parametrize('a,b', [(1,2), (3,4)], ids='case1,case2')
def test_sum(a, b):
assert a + b > 0
ids argumentids parameter expects a list of strings, but here it is given a single string with commas.ids type[(0, 'zero'), (1, 'one'), (2, 'two')] but want to skip the test case where the number is zero using IDs. Which approach correctly uses ids to help identify the skipped test case?Pass the test and this pattern moves unknown