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

后端 未结 5 860
醉酒成梦
醉酒成梦 2020-12-01 17:41

I have multiple tests run by py.test that are located in multiple classes in multiple files.

What is the simplest way to share a large dictionary - which I do not

5条回答
  •  囚心锁ツ
    2020-12-01 18:36

    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.

提交回复
热议问题