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

Allow for overriding of pytest fixture scope #89

Closed
austinkeller opened this issue Dec 15, 2022 · 5 comments · Fixed by #103
Closed

Allow for overriding of pytest fixture scope #89

austinkeller opened this issue Dec 15, 2022 · 5 comments · Fixed by #103

Comments

@austinkeller
Copy link

Being able to override the pytest fixtures to have a "function" scope instead of "session" scope would be useful when using containers that may carry state between tests.

An example of a useful configuration (though hard-coded): Greenlight-Analytical@102eedb

Some boilerplate to connect a flag:

def pytest_addoption(parser):
    parser.addoption(
        "--keep-containers",
        action="store_true",
        help="Keep containers between tests. This is faster but may cause test failures from leftover state between tests.",
    )


@pytest.fixture
def keep_containers(request):
    return request.config.getoption("--keep-containers")


def keep_containers_scope(fixture_name, config):
    if config.getoption("--keep-containers", None):
        return "session"
    return "function"


@pytest.fixture(scope=keep_containers_scope)
# This annotation can apply to all fixtures, instead of the hard-coded "session" scope
@mmetc
Copy link

mmetc commented Jan 13, 2023

Having scope=module on the other hand is useful to run a battery of tests against a set of environments defined in the same file. At least it's a better fit for my use case, otherwise I have to run pytest multiple times.

@austinkeller
Copy link
Author

@mmetc in that case, it'd be better to allow the flag to be set to the desired scope directly, so something like

def pytest_addoption(parser):
    parser.addoption(
        "--containers-scope",
        type=str,
        default="function"
        help="The pytest fixture scope for reusing containers between tests. For available scopes and descriptions, see https://docs.pytest.org/en/6.2.x/fixture.html#fixture-scopes",
    )


@pytest.fixture
def containers_scope_fixture(request):
    return request.config.getoption("--containers-scope")


def containers_scope(fixture_name, config):
    return config.getoption("--containers-scope-fixture", "function"):


@pytest.fixture(scope=containers_scope)
# This annotation can apply to all fixtures, instead of the hard-coded "session" scope

@shaohme
Copy link

shaohme commented May 10, 2023

Having scope=module on the other hand is useful to run a battery of tests against a set of environments defined in the same file. At least it's a better fit for my use case, otherwise I have to run pytest multiple times.

That is true, but sometimes I need different compose.yaml configs for different set of tests. If I haven't got different scopes, I need to run pytest for each set of tests needing the same docker-compose yaml.

@SRv6d
Copy link

SRv6d commented Nov 9, 2023

I would've expected a function scope to be the default, are there any blockers to implementing this?

@BetterCallBene
Copy link
Contributor

I need this for my project. Is it possible that this mr comes into the next release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants