From 21587d62cb9d578ab6a56d6f02b54221b4c20b61 Mon Sep 17 00:00:00 2001 From: open-source-contributer Date: Wed, 16 Sep 2026 15:03:56 +0530 Subject: [PATCH 1/2] Doc: improve discoverability of passing parameters to fixture functions The existing mechanism for passing parameters to fixtures via `request.param` and indirect parametrization is functional but hard to discover. This improves discoverability in three ways: 1. Adds a dedicated "Passing arguments to fixture functions" section to the fixtures HOWTO doc (doc/en/how-to/fixtures.rst) that clearly explains the pattern with a worked example. 2. Updates the `@pytest.fixture()` docstring (src/_pytest/fixtures.py) to mention indirect parametrization and point users to the relevant documentation section. 3. Adds a reference label anchor in doc/en/example/parametrize.rst so the :ref:`indirect parametrization` cross-reference resolves correctly from both the fixture docstring and the new HOWTO section. Fixes #8109 Co-authored-by: open-source-contributer --- doc/en/example/parametrize.rst | 2 ++ doc/en/how-to/fixtures.rst | 47 ++++++++++++++++++++++++++++++++++ src/_pytest/fixtures.py | 18 +++++++++++++ 3 files changed, 67 insertions(+) diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index 8e7e58f9829..02f747566de 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -349,6 +349,8 @@ And then when we run the test: The first invocation with ``db == "DB1"`` passed while the second with ``db == "DB2"`` failed. Our ``db`` fixture function has instantiated each of the DB values during the setup phase while the ``pytest_generate_tests`` generated two according calls to the ``test_db_initialized`` during the collection phase. +.. _`indirect parametrization`: + Indirect parametrization --------------------------------------------------- diff --git a/doc/en/how-to/fixtures.rst b/doc/en/how-to/fixtures.rst index 0ffc0778f9f..cc44a56265c 100644 --- a/doc/en/how-to/fixtures.rst +++ b/doc/en/how-to/fixtures.rst @@ -1260,6 +1260,53 @@ If the data created by the factory requires managing, the fixture can take care customer_3 = make_customer_record("Meredith") +.. _`passing-parameters-to-fixtures`: + + +Passing arguments to fixture functions +----------------------------------------------------------------- + +Sometimes a test needs to configure a fixture in a specific way that +shouldn't affect other tests using the same fixture. For example, a +test might need a database connection to a particular host, or a +service to be started with specific command-line arguments. + +pytest provides a way to pass arguments to fixture functions from +individual tests using :ref:`indirect parametrization +`. The fixture function receives the +argument via the built-in :py:class:`request ` +fixture as :py:attr:`request.param`. + +.. code-block:: python + + import pytest + + @pytest.fixture + def service(request): + # request.param carries the value passed from the test + return f"Service launched with {request.param!r}" + + @pytest.mark.parametrize("service", ["--verbose"], indirect=True) + def test_with_service(service): + assert service == "Service launched with '--verbose'" + +The ``indirect=True`` flag tells pytest that the parametrized argument +``service`` should be passed to the ``service`` fixture function (as +``request.param``) rather than being injected directly into the test +function. + +This is particularly useful when: + +* A fixture has a default configuration but a specific test needs a + different one. +* The parameter value is only relevant to one test and shouldn't cause + other tests using the same fixture to run multiple times. + +For more details and examples, including how to pass parameters to +multiple fixtures at once, see the :ref:`indirect parametrization +` section in the parametrize documentation. + + .. _`fixture-parametrize`: Parametrizing fixtures diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 7656fca2f5b..a7faa21a979 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1567,6 +1567,24 @@ def fixture( of the fixture function and all of the tests using it. The current parameter is available in ``request.param``. + Parameters can also be passed to a fixture from a specific test using + :ref:`indirect parametrization `. This is + useful when a test needs to configure a fixture in a way that shouldn't + affect other tests using the same fixture. For example:: + + import pytest + + @pytest.fixture + def service(request): + return f"Service launched with {request.param!r}" + + @pytest.mark.parametrize("service", ["--verbose"], indirect=True) + def test_with_service(service): + assert service == "Service launched with '--verbose'" + + See the :ref:`indirect parametrization` section in the parametrize + documentation for more details and examples. + :param autouse: If True, the fixture func is activated for all tests that can see it. If False (the default), an explicit reference is needed to activate From 1a40314f59ff10bab6745576140c2eeb827dc0fa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Sep 2026 09:44:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/en/how-to/fixtures.rst | 2 ++ src/_pytest/fixtures.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/doc/en/how-to/fixtures.rst b/doc/en/how-to/fixtures.rst index cc44a56265c..7b1045adf4b 100644 --- a/doc/en/how-to/fixtures.rst +++ b/doc/en/how-to/fixtures.rst @@ -1281,11 +1281,13 @@ fixture as :py:attr:`request.param`. import pytest + @pytest.fixture def service(request): # request.param carries the value passed from the test return f"Service launched with {request.param!r}" + @pytest.mark.parametrize("service", ["--verbose"], indirect=True) def test_with_service(service): assert service == "Service launched with '--verbose'" diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index a7faa21a979..44f3f336ab0 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1574,10 +1574,12 @@ def fixture( import pytest + @pytest.fixture def service(request): return f"Service launched with {request.param!r}" + @pytest.mark.parametrize("service", ["--verbose"], indirect=True) def test_with_service(service): assert service == "Service launched with '--verbose'"