mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-15 04:54:23 +02:00
New autouse fixture to help with test warnings
Refs https://github.com/simonw/sqlite-utils/issues/692#issuecomment-3644371889
This commit is contained in:
parent
81b0599078
commit
77359bea30
5 changed files with 46 additions and 46 deletions
|
|
@ -14,11 +14,29 @@ def pytest_configure(config):
|
|||
sys._called_from_test = True
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def close_all_databases():
|
||||
"""Automatically close all Database objects created during a test."""
|
||||
databases = []
|
||||
original_init = Database.__init__
|
||||
|
||||
def tracking_init(self, *args, **kwargs):
|
||||
original_init(self, *args, **kwargs)
|
||||
databases.append(self)
|
||||
|
||||
Database.__init__ = tracking_init
|
||||
yield
|
||||
Database.__init__ = original_init
|
||||
for db in databases:
|
||||
try:
|
||||
db.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fresh_db():
|
||||
db = Database(memory=True)
|
||||
yield db
|
||||
db.close()
|
||||
return Database(memory=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -32,8 +50,7 @@ def existing_db():
|
|||
INSERT INTO foo (text) values ("three");
|
||||
"""
|
||||
)
|
||||
yield database
|
||||
database.close()
|
||||
return database
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue