Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have two issued fixtures depend on each other?

In test_something(), the app instance should be the same as used by the login instance.

@pytest.fixture
def app():
    # ...
    return app

@pytest.fixture
def login(app):
    # ...
    return login

def test_something(self, app, login):
    pass

What I tried is returning both objects from the second fixture, but I wouldn't call this idiomatic.

@pytest.fixture
def app_and_login(app):
    # ...
    return app, login

def test_something(self, app_and_login):
    app, login = login_and_login

Is there a better way to do this?

like image 978
danijar Avatar asked Oct 27 '25 06:10

danijar


1 Answers

As you described, the fixture already is shared for the runtime of the test by default.

This isn't really documented explicitly anywhere (or at least I haven't found it), but it's somewhat implicit: Sharing a fixture across tests in a module describes the scope parameter, and the default scope is function.

Other scopes would e.g. be module (share/cache the fixture for all tests in the same module) or session (cache the fixture for the whole test session).

like image 101
The Compiler Avatar answered Oct 28 '25 21:10

The Compiler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!