py.test : 在测试后做一些事情

py.test : do something just after a test

对于我的测试,我构建了临时文件,无论测试结果是什么(失败或测试通过)我都想删除这些文件。

有没有办法让 "tell" 到 py.test 在完成测试我的 Python 文件后立即执行某些操作?

这是在官方文档中找到的canvas。

from pytest import fixture

@fixture(scope="module")
def or_datas(request):
    # Something done before a test.

    def fin():
        # Here, just do something after the test.
        ...

    request.addfinalizer(fin)


# How to use it ?

def test_something(or_datas):
    # Note the argument corresponding the function decorated
    # by fixture.
    ...