python - 在整个测试套件或 unittest/pytest 中的 at_exit 之后运行命令的 Hook

标签 python python-3.x pytest python-unittest

我需要在整个测试套件运行后或在整个测试退出时执行命令。我在 unittest 中看到了 tearDown Hook ,但在 ruby​​ 中没有 after-testsuite Hook 或类似 at_exit 的等价物。

unittestpytest 中有没有我可以遵循的方法?或对此进行任何调整

最佳答案

您可以在 pytest 中使用 session 范围的 fixture 。顾名思义,它使您可以在整个测试 session 之前和之后运行代码:

@pytest.fixture(scope='session', autouse=True)
def session_setup_teardown():
    # setup code goes here if needed
    yield
    cleanup_testsuite()

您最好将此 fixture 放入顶层 conftest.py

我不知道 unittest 中有类似的功能 - 最接近的可能是 tearDownModule每个测试模块执行一次。

关于python - 在整个测试套件或 unittest/pytest 中的 at_exit 之后运行命令的 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62628825/

相关文章:

python - 使用 PyLZMA 解压

python - Django,将HTML放入模板时自动出现HTML "sanitizing",如何停止?

python - 如何根据列到值的映射为 DataFrame 赋值?

Python:确定一个字符串是否包含数学?

python - py.test 在从模块导入功能时同时处理 pylint 和 flake8

python - 如何在测试 FastAPI 应用程序时触发生命周期启动和关闭?

python - 从产品图片中删除背景

python - 在 Raspberry Pi3 上将数据从 Python 存储到 MySQL,获取和使用相同数据时遇到问题

python - 使用列表理解删除字符串中的所有元音

python - py.test 将 IndexError/KeyError 报告为失败而不是错误