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(...)