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

Viewed 547

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

1 Answers

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 tearDownModule which is executed once per test module.

Related