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

Geo*_*etz 4 python pickle pytest

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

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

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

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

hoe*_*ing 5

您可以使用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]))
Run Code Online (Sandbox Code Playgroud)

测试

根据您测试的功能,monkeypatchingbuiltins.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
Run Code Online (Sandbox Code Playgroud)