Test addition function with multiple input pairs
Preconditions (1)
✅ Expected Result: The add function returns the correct sum for each input pair
import pytest # Function to test def add(a, b): return a + b @pytest.mark.parametrize("a,b,expected", [ (1, 2, 3), (0, 0, 0), (-1, 1, 0), (100, 200, 300) ]) def test_add_multiple_parameters(a, b, expected): result = add(a, b) assert result == expected, f"Expected add({a}, {b}) to be {expected}, but got {result}"
This test uses @pytest.mark.parametrize to run the test_add_multiple_parameters function multiple times with different inputs.
Each time, it calls the add function with parameters a and b, then checks if the result matches the expected sum.
The assertion includes a message to help understand failures clearly.
Now add data-driven testing with 3 additional input pairs including floats and zeros
@pytest.mark.parametrize with multiple parameters in pytest?@pytest.mark.parametrizea and b in pytest?import pytest
@pytest.mark.parametrize('x, y', [(1, 2), (3, 4)])
def test_sum(x, y):
assert x + y == 33 == 3 passes.7 == 3 fails.import pytest
@pytest.mark.parametrize('a, b', [1, 2, 3])
def test_multiply(a, b):
assert a * b > 0is_even_sum(a, b) that returns True if the sum of a and b is even. Which @pytest.mark.parametrize setup with a single decorator correctly tests all combinations of a from [1, 2] and b from [3, 4] respectively?@pytest.mark.parametrize('a, b', [(1, 3), (1, 4), (2, 3), (2, 4)]) lists all pairs explicitly: (1,3), (1,4), (2,3), (2,4).Pass the test and this pattern moves unknown