pytest如何在测试之间共享全局变量?

发布时间 2023-11-27 14:59:07作者: wmlg

注意:pytest_namespace 现在已弃用

pytest 提供了一种在会话中使用一些全局变量的方法。这些变量也可以被 fixtures 使用。

这些变量是通过 pytest 挂钩控制的。

import pytest

def pytest_namespace():
    return {'my_global_variable': 0}

@pytest.fixture
def data():
    pytest.my_global_variable = 100

def test(data):
    print pytest.my_global_variable