【发布时间】:2021-03-29 07:52:20
【问题描述】:
我正在尝试在 pytest 脚本中使用模块级别的函数(而不是直接将值分配给变量)在 pytest 脚本中定义一个字符串,该函数将在范围 ='module' 的夹具中使用。 问题是,在模块级别使用函数时,它会在 pytest 收集阶段进行评估 - 当在包含几个脚本的文件夹上运行 pytest 时,fixture 使用最后评估的值。
是否有其他方法可以在每个脚本的模块级别设置字符串?
这是我迄今为止所拥有的,但没有完成这项工作:
my_script1.py:
import env_var as env
env.set_my_string('This string will be used in the module fixture')
test_first():
# Do stuff
conf_test.py:
import pytest
import env_var as env
@pytest.fixture(scope='module', autouse=True)
def script_handler():
txt = env.get_my_string()
# Use txt
yield
env_var.py:
_tmp_str = ''
def set_my_string(in_str)
_tmp_str = in_str
def get_my_string():
return _tmp_str
【问题讨论】:
标签: python module pytest fixtures