Skip to content

Fix pytest init#2066

Closed
abondar wants to merge 13 commits intodevelopfrom
fix/pytest-init
Closed

Fix pytest init#2066
abondar wants to merge 13 commits intodevelopfrom
fix/pytest-init

Conversation

@abondar
Copy link
Member

@abondar abondar commented Jan 29, 2026

Fixes #1110
Now we restore default connections after clearing them

AI Summary:

This pull request introduces new tests and improvements to the database test initialization process, specifically targeting better support for pytest and pytest-asyncio workflows. The main focus is on fixing issues with database state management when using the initializer() and adding a new async create_db() helper for cleaner pytest fixture usage. Additionally, it adds comprehensive tests to ensure correct behavior across different database backends and usage patterns.

Key changes:

Testing and CI Enhancements

  • Added a new test file test_pytest_initializer.py with extensive tests for the new async create_db() helper and improved pytest fixture patterns, ensuring isolation and correct DB state across tests.
  • Updated the GitHub Actions CI workflow to run the new initializer tests across all supported databases (SQLite, Postgres, MySQL, MSSQL) to ensure cross-backend compatibility.

New Features and API Improvements

  • Introduced an async create_db() function in tortoise.contrib.test, designed for use in pytest-asyncio fixtures, providing a more reliable and idiomatic way to set up test databases asynchronously. [1] [2]

Bug Fixes and State Management

  • Refactored the initializer() function to avoid clearing connections after initialization, fixing issues where the database became unusable until _restore_default() was called—this resolves problems with pytest fixtures expecting the DB to be ready immediately.
  • Updated finalizer() and test case teardown logic to ensure global state is properly reset after tests, preventing cross-test contamination. [1] [2]

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 29, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing fix/pytest-init (673d1b2) with develop (c08806c)

Summary

✅ 16 untouched benchmarks

@abondar abondar force-pushed the fix/pytest-init branch 2 times, most recently from 7cf8d87 to 44121ed Compare January 29, 2026 20:53
abondar and others added 4 commits January 30, 2026 11:55
When finalizer() sets connections._db_config = None and another test
runs on the same worker (with pytest-xdist), _reset_conn_state() would
fail because it tried to call .clear() on None.

This fix checks if _db_config is not None before clearing it.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
These tests use create_db which resets global state and interferes with
other tests running in parallel. Added pytestmark to skip them when
running with -n auto.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Only skip tests when running with xdist AND in-memory SQLite
- Use TORTOISE_TEST_DB environment variable instead of hardcoded URLs
- Skip file-based SQLite test when running non-SQLite databases
- Tests now run normally in CI for postgres, mysql, etc.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coveralls
Copy link

coveralls commented Jan 30, 2026

Pull Request Test Coverage Report for Build 21523302371

Details

  • 11 of 11 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 85.008%

Totals Coverage Status
Change from base Build 21494023302: 0.003%
Covered Lines: 9845
Relevant Lines: 11198

💛 - Coveralls

@abondar
Copy link
Member Author

abondar commented Feb 3, 2026

Resolved by #2069

@abondar abondar closed this Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problems with pytest supporting in 0.19.0

2 participants