python - py.test : Temporary folder for the session scope

标签 python pytest

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/

相关文章:

python - 使用pyserial与调制解调器通信

python - 选择文本文件中的特定行和单元格并放入数据框 : python or R

python - 可以在 pytest.ini 中设置测试的任意配置吗?

python - Appium中如何获取iOS设备显示的最大宽高坐标?

python - 如何使用 burp 套件代理 HTTPS 流量?

python - 运行时错误: _thnn_mse_loss_forward is not implemented for type torch. cuda.LongTensor

python - Python (Flask) 中的 Get 和 Post 方法

python - 从 protobufs 检查有效的枚举类型

python - 带有 xdist 的 py.test 没有执行用随机值参数化的测试

python - 将 pytest 与 src 层一起使用