orion_tvv orion_tvv - 1 day ago 6
Python Question

Pytest setup/teardown hooks for session

Pytest
has
setup and teardowns
hooks for
module, class, method
.

I want to create my custom test environment in setup (before start of test session) and cleanup after all tests will be finished.
In other words, how can I use hooks like
setup_session and teardown_session
?

Answer

These hooks work well for me.

def pytest_sessionstart(session):
    pass

def pytest_sessionfinish(session, exitstatus):
    pass

But actually next fixture with session scope looks much pretty.

@fixture(autouse=True, scope='session')
def my_fixture():
    # setup_staff
    yield
    # teardown staff
Comments