【问题标题】:Pytest Teardown FixturePytest 拆解夹具
【发布时间】:2021-05-27 14:21:49
【问题描述】:

我需要删除一些为测试而创建的文件夹。 在主测试文件夹中,我创建了一个文件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    

【问题讨论】:

  • 如果要自动参与,需要将fixture设置为autouse=True
  • 非常感谢,完美运行!

标签: python pytest


【解决方案1】:

感谢@MrBean Bremen

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

包装器:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2023-03-11
    • 1970-01-01
    • 2021-01-08
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多