以下内容由ChatGPT生成,仅作记录
在 pytest 的 conftest.py 中,可以通过以下方式设置和使用全局变量:
方法 1:使用 pytest 的 pytest_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
最佳实践
- 简单项目:直接使用 Python 全局变量即可。
- 复杂项目:推荐使用
pytest的fixture,结合scope="session"提供全局变量。 - 动态配置:结合
pytest_addoption动态添加全局变量。
另外,通过 pytest_configure 定义的全局变量 不能直接在 setup 或 setup_class 中使用,因为 pytest_configure 是在测试会话开始时执行的,而 setup 和 setup_class 是在每个测试用例或每个测试类之前执行的。因此,在它们之间的执行顺序上有一定的差异。
不过,有几种方式可以让 pytest_configure 中定义的变量在 setup 或 setup_class 中可用。
1. 通过 pytestconfig Fixture 访问全局配置
pytest_configure 中定义的配置(如全局变量)可以通过 pytestconfig 作为 fixture 在任何测试中访问,包括在 setup 和 setup_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
在 setup 或 setup_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_class 和 setup 都能正确访问它。
2. 使用 pytest 的 config 对象直接传递变量
你可以通过 pytest 的 config 对象将全局变量保存在会话范围内,然后通过 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
在 setup 或 setup_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 可以在 setup 和 setup_class 中访问,避免了通过显式的函数参数传递。
3. 使用 session 范围的 Fixture
通过定义 scope="session" 范围的 fixture,可以确保变量在整个测试会话期间保持有效,并可以在 setup 和 setup_class 中使用。
示例:
# conftest.py
import pytest
@pytest.fixture(scope="session")
def custom_variable():
return "some_global_value"
然后在测试类中的 setup 或 setup_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中定义的全局变量 是在测试会话初始化时执行的,而setup和setup_class在每个测试用例执行之前被调用,因此无法直接访问pytest_configure中定义的变量。- 可以通过
pytestconfigfixture 或scope="session"范围的 fixture 来让setup和setup_class中使用全局变量。 - 推荐通过
pytest的 fixture 系统来显式传递全局变量,以确保可测试性和代码的清晰度。
2204

被折叠的 条评论
为什么被折叠?



