Junior — Middle
How to pass different input data to unit tests using pytest?.
sobes.tech AI
Answer from AI
In pytest, parameterization with the @pytest.mark.parametrize decorator is used to pass different input data to individual tests. This allows running the same test with various sets of arguments.
Example:
import pytest
@pytest.mark.parametrize("input,expected", [
(2, 4),
(3, 9),
(4, 16),
])
def test_square(input, expected):
assert input ** 2 == expected
In this example, the test_square test will be executed three times with different input and expected values. This approach is convenient for testing a function with different input data without duplicating code.