How to run specific code after all tests are executed?

You can use a autouse fixture with session scope:

@pytest.fixture(scope="session", autouse=True)
def db_conn():
    # Will be executed before the first test
    conn = db.connect()
    yield conn
    # Will be executed after the last test
    conn.disconnect()

You can then also use db_conn as an argument to a test function:

def test_foo(db_conn):
    results = db_conn.execute(...)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)