py.test 中的 tmpdir
fixture 使用 function
范围,因此不适用于具有更广泛范围的 fixture ,例如 session
。但是,这对于某些情况很有用,例如设置临时 PostgreSQL 服务器(当然不应该为每个测试重新创建)。
是否有任何干净的方法可以为更广泛的范围获取临时文件夹,而不涉及编写我自己的 fixture 和访问 py.test 的内部 API?
最佳答案
自 pytest 2.8 及更高版本以来, session 范围的 tmpdir_factory
固定装置可用。请参见 documentation 中的以下示例.
# contents of conftest.py
import pytest
@pytest.fixture(scope='session')
def image_file(tmpdir_factory):
img = compute_expensive_image()
fn = tmpdir_factory.mktemp('data').join('img.png')
img.save(str(fn))
return fn
# contents of test_image.py
def test_histogram(image_file):
img = load_image(image_file)
# compute and test histogram
关于python - py.test : Temporary folder for the session scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25525202/