how to share a variable across modules for all tests in py.test

Update: pytest-namespace hook is deprecated/removed. Do not use. See #3735 for details.

You mention the obvious and least magical option: using a fixture. You can apply it to entire modules using pytestmark = pytest.mark.usefixtures('big_dict') in your module, but then it won’t be in your namespace so explicitly requesting it might be best.

Alternatively you can assign things into the pytest namespace using the hook:

# conftest.py

def pytest_namespace():
    return {'my_big_dict': {'foo': 'bar'}}

And now you have pytest.my_big_dict. The fixture is probably still nicer though.

Leave a Comment