Skip to content

Commit 95f8d72

Browse files
committed
Use Config.stash in pytest_session{start,finish}
1 parent a42f7d3 commit 95f8d72

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

ixmp/testing/__init__.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
if TYPE_CHECKING:
7575
from _pytest.mark import ParameterSet
7676
from ixmp4.core import Run
77+
from sqlalchemy import Engine # noqa: F401
7778

7879
try:
7980
# Pint 0.25.1 or later
@@ -108,6 +109,10 @@
108109
#: :any:`True` if testing is occurring on GitHub Actions runners/machines.
109110
GHA = "GITHUB_ACTIONS" in os.environ
110111

112+
# Pytest stash keys
113+
KEY_ENGINE = pytest.StashKey["Engine"]()
114+
KEY_IXMP4_PG_NAME = pytest.StashKey[str]()
115+
111116
_uname = platform.uname()
112117

113118
#: Pytest marks for use throughout the test suite.
@@ -179,7 +184,7 @@ def pytest_sessionstart(session: pytest.Session) -> None:
179184
- :data:`.jdbc._GC_AGGRESSIVE` is set to :any:`False` to disable aggressive garbage
180185
collection, which can be slow.
181186
"""
182-
from ixmp.backend import available, jdbc
187+
from ixmp.backend import jdbc
183188

184189
if not session.config.option.ixmp_user_config:
185190
ixmp_config.clear()
@@ -194,34 +199,29 @@ def pytest_sessionstart(session: pytest.Session) -> None:
194199
from xdist import get_xdist_worker_id
195200

196201
db_name = f"ixmp_test_{get_xdist_worker_id(session)}"
197-
198-
engine = create_engine(
199-
"postgresql+psycopg://postgres:postgres@localhost:5432/postgres",
200-
isolation_level="AUTOCOMMIT",
201-
)
202+
url = "postgresql+psycopg://postgres:postgres@localhost:5432/postgres"
203+
engine = create_engine(url, isolation_level="AUTOCOMMIT")
202204
with engine.connect() as connection:
203205
connection.execute(text(f"CREATE DATABASE {db_name}"))
204206

205-
engine.dispose()
207+
# Store for pytest_sessionfinish()
208+
session.config.stash[KEY_ENGINE] = engine
209+
session.config.stash[KEY_IXMP4_PG_NAME] = db_name
206210

207211

208212
def pytest_sessionfinish(
209213
session: pytest.Session, exitstatus: int | pytest.ExitCode
210214
) -> None:
211-
if "ixmp4" in available():
212-
from sqlalchemy import create_engine, text
213-
from xdist import get_xdist_worker_id
215+
try:
216+
from sqlalchemy import text
214217

215-
db_name = f"ixmp_test_{get_xdist_worker_id(session)}"
218+
engine = session.config.stash[KEY_ENGINE]
219+
db_name = session.config.stash[KEY_IXMP4_PG_NAME]
216220

217-
engine = create_engine(
218-
"postgresql+psycopg://postgres:postgres@localhost:5432/postgres",
219-
isolation_level="AUTOCOMMIT",
220-
)
221221
with engine.connect() as connection:
222222
connection.execute(text(f"DROP DATABASE {db_name}"))
223-
224-
engine.dispose()
223+
except (ImportError, KeyError):
224+
pass
225225

226226

227227
def pytest_report_header(config: pytest.Config, start_path: Path) -> str:

0 commit comments

Comments
 (0)