Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to build dynamic session-scoped fixtures #10156

Open
sirosen opened this issue Jul 19, 2022 · 0 comments
Open

Document how to build dynamic session-scoped fixtures #10156

sirosen opened this issue Jul 19, 2022 · 0 comments
Labels
status: help wanted developers would like help from experts on this topic topic: fixtures anything involving fixtures directly or indirectly type: docs documentation improvement, missing or needing clarification

Comments

@sirosen
Copy link
Contributor

sirosen commented Jul 19, 2022

My particular case was that I wanted a session-scoped fixture which was parametrized by some CLI arguments.
This was much tougher than I expected it to be, and I think a doc example would help. I'm more than happy to write it up, but want to first make sure it's a welcome contribution.

The answer is a variant on the one provided many years ago here:
#2959 (comment)

The only thing which needs a tweak is the self-arg. Combined with py3-only, and fleshed out with a CLI option, it becomes something like:

def pytest_addoption(parser):
    group = parser.getgroup("myopts")
    group.addoption(
        "--servers",
        help="comma-delimited servers to use",
        default="localhost",
        type=str,
    )

def pytest_configure(config):
    server_list = config.getoption("servers").strip().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')


@pytest.mark.parametrize(scope="session")
def server_connection(server_hostname):
    return connect_to_server(server_hostname)
  • Is this a good thing to document?
  • Did I understand the original example correctly?
  • Where should it go?
@Zac-HD Zac-HD added type: question general question, might be closed after 2 weeks of inactivity topic: fixtures anything involving fixtures directly or indirectly labels Jul 24, 2022
@Zac-HD Zac-HD added status: help wanted developers would like help from experts on this topic type: docs documentation improvement, missing or needing clarification and removed type: question general question, might be closed after 2 weeks of inactivity labels Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted developers would like help from experts on this topic topic: fixtures anything involving fixtures directly or indirectly type: docs documentation improvement, missing or needing clarification
Projects
None yet
Development

No branches or pull requests

2 participants