How to access a pytest built-in fixture from a fixture with scope other than 'function'?

Viewed 67

I have read the docs about fixtures a few times and I believe I understand scope:
https://docs.pytest.org/en/6.2.x/fixture.html

I have noticed that all the builtin fixtures I want to use are function scoped, for example: capdf, capsys, monkeypatch, etc...

That pytest documentation above encourages more of the "act step" to be pushed into fixtures instead of test functions.

Doing that without drastically increasing the time required for my tests seems to require the other scopes beyond the "function" scope.

But the builtin fixtures do not work in other scopes. Have I missed a way to use them in other scopes?

Example:

@pytest.fixture(scope="class")
def my_fixture(capfd):
    # ... use capfd to capture stdout
    pass

This causes an error:

ScopeMismatch: You tried to access the function scoped fixture capfd with a class scoped request object

See this workaround for monkeypatch specifically:
https://github.com/pytest-dev/pytest/issues/363#issuecomment-406536200

Is a workaround like this possible for capfd or for all builtin fixtures?

For instance, could one make a capfd_session fixture that uses the CaptureFixture class? Would doing so be inadvisable because it requires going past the API of pytest and these details could be subject to change in a future update?

0 Answers
Related