Pytest撕裂夹具



我需要删除一些为测试而创建的文件夹。在主测试文件夹中,我创建了一个包含以下内容的文件test_teardown.py

import shutil
import pytest

@pytest.fixture(scope="session")
def teardown():
yield
shutil.rmtree('tmp')

然而,在测试会话完成之后,tmp文件夹不会被删除。我用错夹具了吗?

文件结构

+-- Project folder
|   +-- tests
|   |   +-- __init__.py
|   |   +-- test_teardown.py
|   |   +-- Unit
|   |   |   +-- __init__.py
|   |   |   +-- test_moretests.py    

感谢@MrBean Bremen

autouse=True添加到包装器可以确保它自动参与。请确保您的test_teardown.py包含在已使用的测试文件夹中。

包装:

@pytest.fixture(scope='session', autouse=True)
def teardown():
yield
shutil.rmtree('tmp')

最新更新