Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytest:删除被测试函数创建的文件_Python_Pickle_Pytest - Fatal编程技术网

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

Python Pytest:删除被测试函数创建的文件,python,pickle,pytest,Python,Pickle,Pytest,我正在测试一个函数,作为其执行的一部分,它pickle对象。测试之后,我想删除pickle文件 如果是测试本身保存文件,pytest的“tmpdir”装置似乎就是解决方案。但是,由于正在测试的函数是保存文件的创建者,而不是测试,我不确定测试后清理文件的正确方法是什么 在本例中,文件保存在包含正在运行的测试的“tests”目录中。我能想到的唯一选项是在每次测试之后从测试目录中删除所有*.pkl pickle文件。我想知道我是否缺少pytest可能提供的更优雅的解决方案 用pytest测试的函数产生

我正在测试一个函数,作为其执行的一部分,它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
内置.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

因此,问题是,为什么测试中的函数会将文件保存到
tests
目录中?我建议要么修复测试中的代码,要么模拟或以其他方式更改其环境,以将创建的文件放到特定位置。我有一个函数所需的计算密集型矩阵操作系列。如果生成并pickle部分numpy解决方案库,则函数可以更快。然后,每次调用计算时,函数要么加载先前计算的部分解,要么生成它们。对不起,我想我的问题不清楚。我不在乎你为什么要酸洗。我的问题是,为什么要对特定的目录进行pickle?它会对调用它的目录进行pickle。最终目标是创建一个pip可安装的函数库,其中包将这些部分解决方案包含在与库函数相同的目录中。我是PYPIN00B,所以我可能完全不正常,但我的想法是,在打包之前,通过在库文件夹中运行脚本,为最常见的用例生成部分解决方案。当函数未能发现“封装”的部分解决方案时,它会生成并pickle一个。如果是这种情况,那么测试用例只需在调用函数之前执行
chdir()
,让函数将文件放入chdir'ed目录。对吗?