简体   繁体   中英

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

I need to execute a command after the entire testsuite has run or at the exit of the whole tests. I see tearDown hook in unittest but there is no after-testsuite hook or something like at_exit equivalent in ruby.

Is there any approach in unittest or pytest which I can follow; or any tweak on this

You can use a session-scoped fixture in pytest. As the name implies, it gives you the possibility to run code before and after the entire test session:

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

You best put this fixture into the top-level conftest.py .

I'm not aware of a símilar functionality in unittest - the closest is probably tearDownClass which is executed once per test class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM