0
0
PytestHow-ToBeginner ยท 3 min read

How to Parameterize Fixture in pytest: Simple Guide

To parameterize a fixture in pytest, use the @pytest.fixture(params=[...]) decorator with a list of values. Each test using this fixture runs once per parameter, receiving the current value as the fixture argument.
๐Ÿ“

Syntax

The basic syntax to parameterize a fixture uses the @pytest.fixture decorator with the params argument. This argument takes a list of values to pass to the fixture one by one.

  • @pytest.fixture(params=[value1, value2, ...]): Defines the fixture with multiple parameters.
  • def fixture_name(request):: The fixture function receives a special request object.
  • request.param: Accesses the current parameter value inside the fixture.
python
import pytest

@pytest.fixture(params=[1, 2, 3])
def number(request):
    return request.param
๐Ÿ’ป

Example

This example shows a parameterized fixture number that provides values 1, 2, and 3 to the test test_is_positive. The test runs three times, once for each number.

python
import pytest

@pytest.fixture(params=[1, 2, 3])
def number(request):
    return request.param


def test_is_positive(number):
    assert number > 0
Output
collected 3 items test_example.py::test_is_positive[1] PASSED test_example.py::test_is_positive[2] PASSED test_example.py::test_is_positive[3] PASSED
โš ๏ธ

Common Pitfalls

Common mistakes when parameterizing fixtures include:

  • Not using request.param inside the fixture to get the current parameter value.
  • Forgetting to add params list in the @pytest.fixture decorator.
  • Using mutable default arguments or complex objects without proper setup/teardown.

Always ensure the fixture returns or yields the parameter value correctly.

python
import pytest

# Wrong: Missing request.param usage
@pytest.fixture(params=[10, 20])
def wrong_fixture():
    return 5  # Always returns 5, ignoring params


def test_wrong(wrong_fixture):
    assert wrong_fixture in [10, 20]

# Right way
@pytest.fixture(params=[10, 20])
def right_fixture(request):
    return request.param


def test_right(right_fixture):
    assert right_fixture in [10, 20]
Output
collected 2 items test_example.py::test_wrong[10] FAILED test_example.py::test_wrong[20] FAILED test_example.py::test_right[10] PASSED test_example.py::test_right[20] PASSED
๐Ÿ“Š

Quick Reference

Summary tips for parameterizing fixtures in pytest:

  • Use @pytest.fixture(params=[...]) to define parameters.
  • Access current parameter with request.param.
  • Each test using the fixture runs once per parameter.
  • Combine with other fixtures or test parameters for flexible testing.
โœ…

Key Takeaways

Use @pytest.fixture(params=[...]) to run tests with multiple data sets automatically.
Inside the fixture, get the current parameter with request.param.
Each test using the parameterized fixture runs once per parameter value.
Avoid ignoring request.param or missing the params list in the decorator.
Parameterizing fixtures helps write concise, reusable tests for different inputs.