Test function with single parameter using pytest
Preconditions (2)
✅ Expected Result: The test passes for inputs 2 and 4 (returns True) and fails for input 3 (returns False)
import pytest def is_even(number: int) -> bool: return number % 2 == 0 @pytest.mark.parametrize('number,expected', [ (2, True), (3, False), (4, True) ]) def test_is_even(number, expected): result = is_even(number) assert result == expected, f'Expected is_even({number}) to be {expected} but got {result}'
This code defines a simple function is_even that checks if a number is even.
The test function test_is_even uses @pytest.mark.parametrize to run the test three times with different inputs and expected results.
Each time, it calls is_even with the parameter and asserts the output matches the expected value.
This approach keeps tests clean and easy to extend with more inputs.
Now add data-driven testing with 3 different inputs including negative and zero values
import pytest
@pytest.mark.parametrize('x', [2, 4])
def test_double(x):
assert x * 2 == 4import pytest
@pytest.mark.parametrize('val', 1, 2, 3)
def test_positive(val):
assert val > 0def test_is_uppercase(s):
assert s.isupper()Pass the test and this pattern moves unknown