Skip to content

Commit 41f1e21

Browse files
committed
Add pytest initializer tests for various databases in CI configuration
1 parent d4247d7 commit 41f1e21

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ jobs:
6666
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
6767
- name: Run ci
6868
run: make ci
69+
- name: Run pytest initializer tests (no parallelism)
70+
run: |
71+
TORTOISE_TEST_DB="sqlite://:memory:" uv run --frozen pytest tests/contrib/test_pytest_initializer.py -n0 -v --cov=tortoise --cov-append --cov-branch --tb=native
72+
TORTOISE_TEST_DB="asyncpg://postgres:$TORTOISE_POSTGRES_PASS@127.0.0.1:5432/test_{}" uv run --frozen pytest tests/contrib/test_pytest_initializer.py -n0 -v --cov=tortoise --cov-append --cov-branch --tb=native
73+
TORTOISE_TEST_DB="psycopg://postgres:$TORTOISE_POSTGRES_PASS@127.0.0.1:5432/test_{}" uv run --frozen pytest tests/contrib/test_pytest_initializer.py -n0 -v --cov=tortoise --cov-append --cov-branch --tb=native
74+
TORTOISE_TEST_DB="mysql://root:$TORTOISE_MYSQL_PASS@127.0.0.1:3306/test_{}" uv run --frozen pytest tests/contrib/test_pytest_initializer.py -n0 -v --cov=tortoise --cov-append --cov-branch --tb=native
75+
TORTOISE_TEST_DB="mssql://sa:$TORTOISE_MSSQL_PASS@127.0.0.1:1433/test_{}?driver=$TORTOISE_MSSQL_DRIVER&TrustServerCertificate=YES" uv run --frozen pytest tests/contrib/test_pytest_initializer.py -n0 -v --cov=tortoise --cov-append --cov-branch --tb=native
6976
- name: Test FastAPI/Blacksheep Example
7077
run: |
7178
PYTHONPATH=$DEST_FASTAPI uv run --frozen pytest $PYTEST_ARGS $DEST_FASTAPI/_tests.py

tests/contrib/test_pytest_initializer.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
This broke pytest fixtures that expected the DB to be ready after initializer().
1010
1111
NOTE: These tests are skipped when running with pytest-xdist in parallel mode
12-
AND in-memory SQLite, because in-memory SQLite creates a new DB per connection,
13-
so create_db/drop_databases interferes with other tests on the same worker.
14-
For persistent DBs (postgres, mysql, file-based sqlite), tests run normally.
15-
For in-memory SQLite, run separately with: pytest tests/contrib/test_pytest_initializer.py -n0
12+
because create_db/drop_databases resets global Tortoise state (including event loop
13+
bindings on connections), which interferes with other tests on the same worker.
14+
Run them separately with: pytest tests/contrib/test_pytest_initializer.py -n0
1615
"""
1716

1817
import os
@@ -35,31 +34,16 @@ def get_test_db_url() -> str:
3534
return os.environ.get("TORTOISE_TEST_DB", "sqlite://:memory:")
3635

3736

38-
def is_memory_sqlite() -> bool:
39-
"""Check if we're using in-memory SQLite."""
40-
return ":memory:" in get_test_db_url()
41-
42-
4337
def is_sqlite() -> bool:
4438
"""Check if we're using SQLite (any variant)."""
4539
return get_test_db_url().startswith("sqlite:")
4640

4741

48-
def should_skip_initializer_tests() -> bool:
49-
"""
50-
Skip these tests when running with xdist AND in-memory sqlite.
51-
52-
The issue is that in-memory sqlite creates a new DB per connection,
53-
so create_db/drop_databases interferes with other tests on the same worker.
54-
For persistent DBs (postgres, mysql, file-sqlite), this isn't an issue.
55-
"""
56-
return is_xdist_worker() and is_memory_sqlite()
57-
58-
59-
# Skip only when running with xdist AND in-memory sqlite
42+
# Skip when running with xdist - create_db/drop_databases resets global state
43+
# (including event loop bindings) which interferes with other tests on same worker
6044
pytestmark = pytest.mark.skipif(
61-
should_skip_initializer_tests(),
62-
reason="These tests use create_db which resets global state; with in-memory sqlite run separately with -n0",
45+
is_xdist_worker(),
46+
reason="These tests use create_db which resets global state; run separately with -n0",
6347
)
6448

6549

0 commit comments

Comments
 (0)