diff --git a/tests/conftest.py b/tests/conftest.py index d4ab92f4..705c6368 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,11 +15,21 @@ @pytest.fixture -def app(request: pytest.FixtureRequest, tmp_path: Path) -> Flask: +def app(request: pytest.FixtureRequest, tmp_path: Path) -> t.Generator[Flask]: app = Flask(request.module.__name__, instance_path=str(tmp_path / "instance")) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" app.config["SQLALCHEMY_RECORD_QUERIES"] = False - return app + yield app + + if app.extensions: + db = app.extensions["sqlalchemy"] + # Duplicate SQLAlchemy.engines logic to avoid errors when the app + # is not initialized properly. + if app in db._app_engines: + engines = db._app_engines[app] + if engines: + for engine in engines.values(): + engine.dispose() @pytest.fixture diff --git a/tests/test_metadata.py b/tests/test_metadata.py index 8b54e5bc..523e59af 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -156,6 +156,10 @@ def test_reflect(app: Flask) -> None: db.Table("post", sa.Column("id", sa.Integer, primary_key=True), bind_key="post") db.create_all() + # Dispose engines to close connections and avoid warning + for engine in db.engines.values(): + engine.dispose() + del app.extensions["sqlalchemy"] db = SQLAlchemy(app) assert not db.metadata.tables