Test addition function with multiple inputs using pytest parametrize with IDs
Preconditions (2)
✅ Expected Result: All test cases pass and the test report shows each test case with its custom ID
import pytest @pytest.mark.parametrize( "a, b, expected", [ (1, 2, 3), (5, 5, 10), (10, 0, 10) ], ids=["one_plus_two", "five_plus_five", "ten_plus_zero"] ) def test_addition(a, b, expected): result = a + b assert result == expected, f"Expected {a} + {b} to be {expected}, but got {result}"
This test uses pytest.mark.parametrize to run the test_addition function three times with different inputs.
The ids parameter assigns friendly names to each test case. This helps when reading test reports because you see names like one_plus_two instead of just numbers.
The assertion checks if the sum is correct and gives a clear message if it fails.
This approach keeps tests simple, readable, and easy to maintain.
Now add data-driven testing with 3 different inputs including negative numbers and zero, each with descriptive IDs
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