Junior — Middle
How do you implement parameterization of tests when using Python?
sobes.tech AI
Answer from AI
In Python, test parameterization is often implemented using the pytest library, utilizing the @pytest.mark.parametrize decorator. This 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 executed three times with different input and expected values. This approach simplifies writing and maintaining tests, increases coverage, and improves readability.