pythonpytest

Pytest: run a function at the end of the tests


I would like to run a function at the end of all the tests.

A kind of global teardown function.

I found an example here and some clues here but it doesn't match my need. It runs the function at the beginning of the tests. I also saw the function pytest_runtest_teardown(), but it is called after each test.

Plus: if the function could be called only if all the tests passed, it would be great.


Solution

  • I found:

    def pytest_sessionfinish(session, exitstatus):
        """ whole test run finishes. """
    

    exitstatus can be used to define which action to run. pytest docs about this