Python > Testing in Python > pytest > Parametrization

Basic Pytest Parametrization

This snippet demonstrates basic pytest parametrization using the @pytest.mark.parametrize decorator. It allows you to run the same test function multiple times with different inputs, simplifying testing of various scenarios.

Code Snippet

This code defines a test function test_square that is parametrized using @pytest.mark.parametrize. The decorator takes two arguments: a string of comma-separated parameter names (input_value, expected_result) and a list of tuples. Each tuple represents a set of values for the parameters. Pytest will run the test_square function once for each tuple in the list, substituting the values from the tuple into the corresponding parameters. In this example, it will test if the square of 2 is 4, the square of 3 is 9, the square of 4 is 16, and the square of 5 is 25.

import pytest

@pytest.mark.parametrize("input_value, expected_result", [
    (2, 4),
    (3, 9),
    (4, 16),
    (5, 25)
])
def test_square(input_value, expected_result):
    assert input_value * input_value == expected_result

Concepts Behind the Snippet

Parametrization is a powerful testing technique that allows you to avoid writing repetitive test code. Instead of writing separate test functions for each input value, you can use parametrization to run the same test function with different inputs. This makes your test code more concise, maintainable, and easier to read. The @pytest.mark.parametrize decorator is a key part of pytest that makes this possible.

Real-Life Use Case

Imagine you are testing a function that calculates shipping costs based on weight and destination. You can use parametrization to test the function with different weights and destinations, ensuring that it calculates the correct shipping cost for each combination. Another use case is testing functions dealing with boundary conditions - such as when inputs are near zero, very large numbers or special characters.

Best Practices

  • Keep your parametrization lists concise and focused on testing specific scenarios.
  • Use meaningful parameter names to improve readability.
  • Consider using helper functions to generate complex parameter lists.
  • If the number of parameters becomes unmanageable consider refactoring the code to simplify input.

Interview Tip

When discussing parametrization in a job interview, emphasize its ability to reduce code duplication and improve test maintainability. Explain how it can be used to test different scenarios with a single test function. Be prepared to provide real-world examples of how you have used parametrization in your projects.

When to use them

Use parametrization when you need to test the same functionality with multiple different inputs or configurations. It's particularly useful when testing edge cases, boundary conditions, and different data types. Avoid using it when the test logic itself is significantly different for each set of parameters, as this might indicate a need for separate test functions.

Memory footprint

Parametrization generally has a small memory footprint. The parameters are passed to the test function at runtime. However, extremely large parameter lists could potentially consume a significant amount of memory, especially if the parameters themselves are large objects. Consider generating parameters on the fly using generators if memory usage becomes a concern.

Alternatives

  • Looping: You can achieve similar results by looping through a list of inputs within a test function. However, this approach does not provide the same level of reporting and integration with pytest as parametrization.
  • Data-driven testing frameworks: Other testing frameworks offer data-driven testing capabilities that are similar to pytest's parametrization.

Pros

  • Reduced code duplication
  • Improved test readability and maintainability
  • Easy to test multiple scenarios with a single test function
  • Excellent integration with pytest's reporting and debugging tools

Cons

  • Can become complex to manage with a large number of parameters
  • Requires careful planning to ensure that all relevant scenarios are covered
  • Debugging can be challenging if a test fails with a specific set of parameters

FAQ

  • How do I run a specific set of parameters?

    You can use the -k option to filter tests based on parameter values. For example, pytest -k "input_value==3" will run only the test with input_value equal to 3.
  • Can I parametrize multiple test functions?

    Yes, you can use the @pytest.mark.parametrize decorator on multiple test functions.
  • Can I use different data types in my parameters?

    Yes, you can use any valid Python data type in your parameters, including integers, floats, strings, lists, and dictionaries.