Skip to content

doc: add example for dynamic session-scoped parametrized fixtures#14320

Open
sedat4ras wants to merge 1 commit intopytest-dev:mainfrom
sedat4ras:docs/dynamic-session-fixtures
Open

doc: add example for dynamic session-scoped parametrized fixtures#14320
sedat4ras wants to merge 1 commit intopytest-dev:mainfrom
sedat4ras:docs/dynamic-session-fixtures

Conversation

@sedat4ras
Copy link

Closes #10156

Summary

Adds a documentation example to doc/en/example/special.rst showing how to build session-scoped parametrized fixtures whose params depend on command-line arguments.

The problem: A plain @pytest.fixture definition with params cannot use CLI option values because params are evaluated at import time — before pytest_configure runs. This trips up many users (the workaround was buried in a 2017 issue comment).

The solution: Register a plugin class containing the fixture inside pytest_configure, where CLI options are already available.

def pytest_configure(config):
    server_list = [s.strip() for s in config.getoption("--servers").split(",")]

    class DynamicFixturePlugin:
        @pytest.fixture(scope="session", params=server_list)
        def server_hostname(self, request):
            return request.param

    config.pluginmanager.register(DynamicFixturePlugin(), "server-hostname-fixture")

The example was adapted from the working code provided by the issue author in #10156.

Checklist

AI attribution: This PR was written with AI assistance (Claude). The changes have been reviewed for correctness against pytest's fixture and plugin documentation.

Closes pytest-dev#10156

Show how to build a session-scoped parametrized fixture whose params
depend on command-line arguments. A plain @pytest.fixture definition
cannot do this because params are evaluated at import time, before
pytest_configure runs. The solution is to register a plugin class
inside pytest_configure, where CLI options are already available.

Co-authored-by: Claude <noreply@anthropic.com>
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document how to build dynamic session-scoped fixtures

1 participant