Python > Testing in Python > pytest > Fixtures in pytest
Parameterized Fixtures
This example demonstrates how to parameterize a pytest fixture to provide different data sets to the same test function.
Code Snippet
The @pytest.fixture(params=[1, 2, 3])
decorator turns the number
function into a parameterized fixture. The params
argument is a list of values that will be passed to the fixture. The request
object provides access to the current parameter value through request.param
. The test_number_is_even
function will be executed three times, once for each value in the params
list. Pay attention that the test will not fail in this example. The print statement lets us see the data.
import pytest
@pytest.fixture(params=[1, 2, 3])
def number(request):
# Access the parameter value using request.param
return request.param
def test_number_is_even(number):
# Test function that uses the parameterized number fixture
print(f"\nTesting with number: {number}")
assert number % 2 == 0 or number % 2 != 0
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")
# Example Usage (Run with pytest)
Concepts Behind the Snippet
Parameterized fixtures allow you to run the same test function with different inputs. This is useful for testing edge cases, boundary conditions, or different configurations of your code. By defining a list of parameters, you can avoid duplicating test code and ensure that your tests cover a wide range of scenarios.
Real-Life Use Case Section
Consider testing a function that calculates discounts based on user type. You could use a parameterized fixture to provide different user types (e.g., 'new', 'returning', 'premium') and their corresponding discount rates. The same test function could then verify that the correct discount is applied for each user type. This is way better than writing three different test functions.
Best Practices
Interview Tip
When discussing parameterized fixtures, highlight their ability to reduce code duplication and improve test coverage. Explain how you've used them to test various scenarios or configurations within your projects. Be prepared to discuss the trade-offs between using parameterized fixtures and writing separate test functions for each scenario.
When to Use Them
Use parameterized fixtures when you need to test the same function or code with different inputs or configurations. This is particularly useful for testing functions with multiple parameters or for exploring edge cases and boundary conditions.
Alternatives
An alternative is to use the @pytest.mark.parametrize
decorator directly on the test function. However, using parameterized fixtures is better if the parameterization logic needs to be reused across multiple test functions or if setup/teardown is required for each parameter.
Pros
Cons
FAQ
-
Can I use fixtures within parameterized fixtures?
Yes, you can combine fixtures in any way that suits your testing needs. This allows you to create complex setups and teardowns that are tailored to specific test scenarios. -
How do I access the parameter value within the test function?
The parameterized fixture is passed as an argument to the test function. You can then access the parameter value directly through the fixture argument.