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