pytest使用tmp目录和文件

使用tmp目录和文件

tmp_path Fixture方法

3.9版本新函数
你可以使用tmp_path 在临时目录根目录中创建一个独立的临时目录以供测试调用。

tmp_path是一个pathlib/pathlib2.Path对象。以下是测试使用方法的示例如:

# test_tmp_path.py文件内容
import os

CONTENT = u"content"

def test_create_file(tmp_path):
    d = tmp_path / "sub"
    d.mkdir()
    p = d / "hello.txt"
    p.write_text(CONTENT)
    assert p.read_text() == CONTENT
    assert len(list(tmp_path.iterdir())) == 1
    assert 0

运行这个,我们可以看到,除了assert 0这一行,其他断言都正常测试通过:

$ pytest test_tmpdir.py
=========================== test session starts ============================
platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR,inifile:
collected 1 item

test_tmpdir.py F                                                     [100%]

================================= FAILURES =================================
_____________________________ test_create_file _____________________________

tmpdir = local('PYTEST_TMPDIR/test_create_file0')

    def test_create_file(tmpdir):
        p = tmpdir.mkdir("sub").join("hello.txt")
        p.write("content")
        assert p.read() == "content"
        assert len(tmpdir.listdir()) == 1
>       assert 0
E       assert 0

test_tmpdir.py:7: AssertionError
========================= 1 failed in 0.12 seconds =========================

fixture: tmpdir 方法

tmpdir可以为测试用例提供一个唯一的临时文件夹,这个文件夹位于base temporary directory
(8.5). tmpdir是一个py.path.local对象,可以使用os.path的功能.示例如下:
def test_create_file_tmpdir(tmpdir):

    p = tmpdir.mkdir("sub").join("Test.txt")
    print("************",p)
    p.write("pytestContent")
    assert p.read() == "pytestContent"
    assert len(tmpdir.listdir()) == 1
    assert 0
运行该测试可以发现除了最后用来观察结果的assert 0是失败的,其他的步骤都是pass的。
$ pytest test_tmpdir.py =========================== test session starts ============================ 
platform linux ‐‐ Python 3.x.y, pytest‐4.x.y, py‐1.x.y, pluggy‐0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache 
rootdir: $REGENDOC_TMPDIR, inifile: 
collected 1 item test_tmpdir.py F 
[100%] ================================= FAILURES ================================= 
_____________________________ test_create_file _____________________________ 
tmpdir = local('PYTEST_TMPDIR/test_create_file0') 
   def test_create_file(tmpdir): 
       p = tmpdir.mkdir("sub").join("hello.txt")
       p.write("content") 
       assert p.read() == "content" 
       assert len(tmpdir.listdir()) == 1 
       > assert 0
       E assert 0 
       test_tmpdir.py:7: AssertionError
       ========================= 1 failed in 0.12 seconds =========================

tmp_path_factory Fixture方法

2.8版本新函数
tmpdir_factory是一个session范围的fixture,可用于从任何其他测试用例及fixture中创建任意临时目录。

例如,假设你的测试套件需要使用程序动态生成在本地磁盘上的一个大图片,你可以整个测试session中只生成一次以节省时间,而不是为每个用例都在自己的tmpdir中计算并生成一次:

# 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)
    # 计算和测试histogram

 

posted @ 2020-10-30 17:05  小L小  阅读(315)  评论(0)    收藏  举报