Here's a minimal example of a behaviour that I "discovered":
import pytest
@pytest.fixture(scope="function")
def introduce_yourself(my_name):
return f"Hello, my name is {my_name}"
@pytest.mark.parametrize("my_name", ["Alice", "Bob"])
def test_introduction(introduce_yourself):
print(introduce_yourself)
When running pytest
, it yields the results:
platform linux -- Python 3.10.11, pytest-7.3.1, pluggy-1.0.0
Using --randomly-seed=37854407
rootdir: /home/REDACTED
configfile: pyproject.toml
plugins: mock-3.10.0, cov-4.0.0, randomly-3.12.0
collected 2 items
REDACTED/tryout.py::test_introduction[Alice] Hello, my name is Alice
PASSED
REDACTED/tryout.py::test_introduction[Bob] Hello, my name is Bob
PASSED
So it seems the parametrization of test_introduction
is passed on to the fixture introduce_yourself
.
I found this behaviour described in a medium.com article but I cannot find anything in the pytest documentation. Is this behaviour intentional and can be relied upon?
Thanks and cheers!