在整个测试套件或 unittest / pytest 中的 at_exit 之后挂钩到 运行 命令

Hooks to run commands after the entire testsuite or at_exit in unittest / pytest

我需要在整个测试套件 运行 之后或在整个测试退出时执行命令。我在 unittest 中看到 tearDown 挂钩,但是在 ruby.

中没有测试套件挂钩或类似 at_exit 的等价物

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

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

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

你最好把这个夹具放到顶层conftest.py

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