Sobes.tech
Junior — Middle

Can you explain what is meant by using parameters in tests with pytest?

sobes.tech AI

Answer from AI

Test parameterization in pytest allows running the same test with different sets of input data, simplifying the testing of multiple scenarios without code duplication.

This is achieved using the @pytest.mark.parametrize decorator, which takes the parameter name and a list of values or tuples of values.

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 run three times with different input and expected values. This is useful for testing a function with various data.