Verify addition function with multiple input sets
Preconditions (2)
✅ Expected Result: All tests pass for each input set, confirming the addition function works correctly for multiple cases
import pytest def add(a, b): return a + b @pytest.mark.parametrize("a,b,expected", [ (1, 2, 3), (0, 0, 0), (-1, 1, 0) ]) def test_add(a, b, expected): result = add(a, b) assert result == expected, f"Expected {expected} but got {result}"
This test uses @pytest.mark.parametrize to run test_add three times with different inputs.
Each time, it checks if the add function returns the expected sum.
This multiplies test coverage by testing multiple cases with one test function, making tests concise and clear.
Now add data-driven testing with 3 different input sets including floats and large numbers
@pytest.mark.parametrize in tests?@pytest.mark.parametrize decorator runs one test function multiple times with different input values.a and b in pytest?@pytest.mark.parametrize with a string of parameter names and a list of tuples for values.@pytest.mark.parametrize('a,b', [(1, 2), (3, 4)]). Others have wrong names or formats.import pytest
@pytest.mark.parametrize('x', [0, 1, 2])
@pytest.mark.parametrize('y', [10, 20])
def test_sum(x, y):
assert x + y > 10
@pytest.mark.parametrize('num', [1, 2, 3])
def test_is_even(num):
assert num % 2 == 0
@pytest.mark.parametrize decorators on a single test function affect test coverage and execution?Pass the test and this pattern moves unknown