pytest conftest.py中如何设置全局变量

以下内容由ChatGPT生成,仅作记录

pytestconftest.py 中,可以通过以下方式设置和使用全局变量:

方法 1:使用 pytestpytest_configure

pytest_configure 是一个特殊的 hook,可以用于设置全局变量。

示例:
# conftest.py
def pytest_configure(config):
    config.global_data = {"key": "value"}

在测试用例中访问:
# test_example.py
def test_global(pytestconfig):
    assert pytestconfig.global_data["key"] == "value"
  • pytestconfig 是 pytest 提供的内置 fixture,包含 pytest_configure 中定义的内容。

方法 2:使用 Python 的全局变量

直接定义全局变量,适用于简单场景。

示例:
# conftest.py
global_variable = {"key": "value"}

def pytest_configure(config):
    global global_variable
    global_variable["another_key"] = "another_value"

在测试用例中访问:
# test_example.py
from conftest import global_variable

def test_global_variable():
    assert global_variable["key"] == "value"
    assert global_variable["another_key"] == "another_value"


方法 3:通过自定义 Fixture 提供全局变量

使用 pytest fixture 是一种更灵活和推荐的方法,可以通过 scope="session" 设置全局范围。

示例:
# conftest.py
import pytest

@pytest.fixture(scope="session")
def global_data():
    return {"key": "value"}

在测试用例中使用:
# test_example.py
def test_global_fixture(global_data):
    assert global_data["key"] == "value"


方法 4:结合 pytestconfig 和自定义变量

pytestconfig 可以作为参数,结合 addoption 动态传递参数或全局变量。

示例:
# conftest.py
def pytest_addoption(parser):
    parser.addoption("--global-key", action="store", default="default_value")

@pytest.fixture(scope="session")
def global_key(pytestconfig):
    return pytestconfig.getoption("global-key")

在测试用例中使用:
# test_example.py
def test_global_key(global_key):
    assert global_key == "default_value"

执行时指定值:
pytest --global-key=custom_value


最佳实践

  1. 简单项目:直接使用 Python 全局变量即可。
  2. 复杂项目:推荐使用 pytestfixture,结合 scope="session" 提供全局变量。
  3. 动态配置:结合 pytest_addoption 动态添加全局变量。

另外,通过 pytest_configure 定义的全局变量 不能直接在 setupsetup_class 中使用,因为 pytest_configure 是在测试会话开始时执行的,而 setupsetup_class 是在每个测试用例或每个测试类之前执行的。因此,在它们之间的执行顺序上有一定的差异。

不过,有几种方式可以让 pytest_configure 中定义的变量在 setupsetup_class 中可用。

1. 通过 pytestconfig Fixture 访问全局配置

pytest_configure 中定义的配置(如全局变量)可以通过 pytestconfig 作为 fixture 在任何测试中访问,包括在 setupsetup_class 中。

示例:
# conftest.py
def pytest_configure(config):
    config.custom_variable = "some_global_value"

@pytest.fixture(scope="session")
def custom_variable(pytestconfig):
    return pytestconfig.custom_variable

setupsetup_class 中使用:
# test_example.py
class TestExample:
    @classmethod
    def setup_class(cls, custom_variable):
        print(f"Custom Variable in setup_class: {custom_variable}")
        assert custom_variable == "some_global_value"
    
    def setup(self, custom_variable):
        print(f"Custom Variable in setup: {custom_variable}")
        assert custom_variable == "some_global_value"

    def test_case(self):
        assert True

这里,custom_variable 是通过 pytestconfig 获取的全局配置,setup_classsetup 都能正确访问它。


2. 使用 pytestconfig 对象直接传递变量

你可以通过 pytestconfig 对象将全局变量保存在会话范围内,然后通过 fixture 提供给每个测试或测试类。

示例:
# conftest.py
def pytest_configure(config):
    config.custom_variable = "some_global_value"

@pytest.fixture(scope="session")
def custom_variable(pytestconfig):
    return pytestconfig.custom_variable

setupsetup_class 中,可以通过 pytestconfig 来访问:

# test_example.py
class TestExample:
    @classmethod
    def setup_class(cls, pytestconfig):
        print(f"Custom Variable in setup_class: {pytestconfig.custom_variable}")
        assert pytestconfig.custom_variable == "some_global_value"
    
    def setup(self, pytestconfig):
        print(f"Custom Variable in setup: {pytestconfig.custom_variable}")
        assert pytestconfig.custom_variable == "some_global_value"

    def test_case(self):
        assert True

通过 pytestconfig 传递的 custom_variable 可以在 setupsetup_class 中访问,避免了通过显式的函数参数传递。


3. 使用 session 范围的 Fixture

通过定义 scope="session" 范围的 fixture,可以确保变量在整个测试会话期间保持有效,并可以在 setupsetup_class 中使用。

示例:
# conftest.py
import pytest

@pytest.fixture(scope="session")
def custom_variable():
    return "some_global_value"

然后在测试类中的 setupsetup_class 中使用:

# test_example.py
class TestExample:
    @classmethod
    def setup_class(cls, custom_variable):
        print(f"Custom Variable in setup_class: {custom_variable}")
        assert custom_variable == "some_global_value"
    
    def setup(self, custom_variable):
        print(f"Custom Variable in setup: {custom_variable}")
        assert custom_variable == "some_global_value"

    def test_case(self):
        assert True

总结

  • pytest_configure 中定义的全局变量 是在测试会话初始化时执行的,而 setupsetup_class 在每个测试用例执行之前被调用,因此无法直接访问 pytest_configure 中定义的变量。
  • 可以通过 pytestconfig fixturescope="session" 范围的 fixture 来让 setupsetup_class 中使用全局变量。
  • 推荐通过 pytest 的 fixture 系统来显式传递全局变量,以确保可测试性和代码的清晰度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值