我正在构建一个pytest测试套件,测试我正在构建的包。测试应该验证一些关于pkg的内容,例如次要的功能检查、次要的正常检查等等。有些检查是预先安装的,有些是安装后的(示例代码):
测试文件:
import pytest
class PreInstallTests(object):
def test_foo(self, bar):
assert bar > 2
class PostInstallTests(object):
def test_goo(self, baz):
assert baz < 3
def test_hoo(self, baz):
assert baz > 1口供档案:
import pytest
@pytest.fixture
def tmp_dir():
os.mkdir('tmp')
yield
shutil.rmtree('tmp')
@pytest.fixture
def bar(tmp_dir):
yield 3
@pytest.fixture
def baz(install, tmp_dir):
yield 2
@pytest.fixture(scope='session')
def install():
# installs the pkg... (takes a lot of time)从这里可以看到,我有两个安装后测试,每个测试使用一个名为baz的夹具,它使用install夹具。我只想有一个安装,或者对所有安装后测试使用相同的安装(不理想,但测试是最小的,我不想浪费太多的时间在重新安装目前)。
使用“作用域”会话参数可以解决我的问题,但这将禁止我使用任何其他夹具,例如"tmp_dir“(它代表我需要的一个夹具,必须保持默认范围.)
如何实现所有测试的一个安装,并仍然使用我的其他设备,我想离开他们的默认作用域?
我想让install成为一个自动安装工具,但实际上我需要在当前默认范围内的其他一些固定装置之后调用它,并且应该保持这种状态。
发布于 2019-09-17 23:44:59
我所能想到的唯一解决方案是,如果您已经运行了安装代码,就会有一个标志控制,然后保持夹具具有默认范围。
import pytest
installed = False
@pytest.fixture()
def install():
global installed
if not installed:
print('installing')
installed = True发布于 2021-08-06 21:53:47
如ScopeMismatch on using session scoped fixture with pytest-mozwebqa plugin for py.test所述
使用会话作用域tmpdir_factory安装和删除tmp_dir,使用内置tmpdir,因为pytest 3.x。
https://stackoverflow.com/questions/57939928
复制相似问题