Pytest:删除由测试函数创建的文件

Pytest: deleting files created by the tested function

我正在测试一个函数,作为其执行的一部分,pickle 对象。测试后,我想删除pickle文件

如果是测试本身保存文件,pytest 的“tmpdir”夹具似乎是解决方案。但是,由于正在测试的函数是保存文件的创建者,而不是测试,我不确定测试后清理文件的正确方法是什么。

在这种情况下,文件保存在包含测试的“tests”目录中运行。我能想到的唯一选择是在每次测试后从测试目录中删除所有 *.pkl pickle 文件。我想知道我是否缺少 pytest 可能提供的更优雅的解决方案。

清理作为使用 pytest 测试的函数的副作用而生成的任何文件的标准方法是什么?

可以使用monkeypatch文件打开功能查看是否写入了新文件。在列表中收集新文件。然后,浏览列表并删除文件。示例:

# spam.py
import pathlib
import numpy

def plain_write():
    with open('spam.1', 'w') as f:
        f.write('eggs')


def pathlib_write():
    with pathlib.Path('spam.2').open('w') as f:
        f.write('eggs')


def pathlib_write_text():
    pathlib.Path('spam.3').write_text('eggs')


def pathlib_write_bytes():
    pathlib.Path('spam.3').write_bytes(b'eggs')


def numpy_save():
    numpy.save('spam.4', numpy.zeros([10, 10]))

def numpy_savetxt():
    numpy.savetxt('spam.5', numpy.zeros([10, 10]))

测试

根据您测试的功能,monkeypatching builtins.open 可能还不够:例如,要清理使用 pathlib 编写的文件,您还需要 monkeypatch io.open.

import builtins
import io
import os
import pytest
import spam


def patch_open(open_func, files):
    def open_patched(path, mode='r', buffering=-1, encoding=None, 
                    errors=None, newline=None, closefd=True,
                    opener=None):
        if 'w' in mode and not os.path.isfile(path):
            files.append(path)
        return open_func(path, mode=mode, buffering=buffering, 
                         encoding=encoding, errors=errors,
                         newline=newline, closefd=closefd, 
                         opener=opener)
    return open_patched


@pytest.fixture(autouse=True)
def cleanup_files(monkeypatch):
    files = []
    monkeypatch.setattr(builtins, 'open', patch_open(builtins.open, files))
    monkeypatch.setattr(io, 'open', patch_open(io.open, files))
    yield
    for file in files:
        os.remove(file)


def test_plain_write():
    assert spam.plain_write() is None


def test_pathlib_write():
    assert spam.pathlib_write() is None


def test_pathlib_write_text():
    assert spam.pathlib_write_text() is None


def test_pathlib_write_bytes():
    assert spam.pathlib_write_bytes() is None


def test_numpy_save():
    assert spam.numpy_save() is None


def test_numpy_savetxt():
    assert spam.numpy_savetxt() is None