Skip to content

fix: show config errors once at session start instead of per-test#55

Merged
croc100 merged 3 commits into
mainfrom
fix/error-message-quality
Jun 8, 2026
Merged

fix: show config errors once at session start instead of per-test#55
croc100 merged 3 commits into
mainfrom
fix/error-message-quality

Conversation

@croc100

@croc100 croc100 commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Problem

When alembic.ini was missing, pytest-mrt produced the same error for
every collected test. A default project has 7 built-in tests, so the
output was 7 identical blocks each with a double traceback:

ERROR at setup of test_mrt_single_head
alembic.ini not found: 'alembic.ini'
...Django hint...
During handling of the above exception, another exception occurred:
alembic.ini not found: 'alembic.ini'
...same message again...

(repeated 7 times)

Fix

  • Add pytest_sessionstart hook that validates MRTConfig once before
    any test runs. Fatal config errors call pytest.exit() -- one clear
    message, session stops immediately.
  • Break the exception chain in the mrt fixture by capturing the error
    string before calling pytest.fail(). This eliminates the "During
    handling of the above exception" noise.
  • Simplify MRTFixture.__init__ error message: the Django hint is now
    shown only via pytest_sessionstart (not duplicated in the fixture).

Before / After

Before:

ERROR at setup of test_mrt_single_head    <- x7
alembic.ini not found: 'alembic.ini'
...
During handling of the above exception, another exception occurred:
alembic.ini not found: 'alembic.ini'
...

After:

Exit: alembic.ini not found: 'alembic.ini'

Check the path and update MRTConfig(alembic_ini=...) in your conftest.py.

If you are using Django migrations (not Alembic), set django_settings instead:
    ...

See: https://croc100.github.io/pytest-mrt/quickstart/

Test plan

  • All existing tests pass (431 passed, 17 skipped)
  • Updated test_mrt_fixture_raises_on_missing_alembic_ini to match revised message
  • Manual: pytest test_migrations.py with missing alembic.ini shows one error and exits cleanly

croc100 added 3 commits June 8, 2026 19:40
- pre-commit section: add args example for Alembic and Django, note on
  pre-commit autoupdate, update rev from v1.3.0 to v1.2.0
- Remove (v1.3.0) version labels from section headers for unreleased features
- Update 'What's new' section to reflect v1.2.0 accurately

Closes #35
…d next steps

Three issues fixed:

1. db_url quoting bug: when a user typed a literal URL (e.g. sqlite:///test.db),
   it was written as db_url=sqlite:///test.db without quotes — invalid Python.
   Now correctly written as db_url="sqlite:///test.db".

2. Confusing default prompt: the old default was the raw Python expression
   os.environ.get("TEST_DATABASE_URL", "sqlite:///test.db") which is unclear
   for first-time users. Default is now simply sqlite:///test.db with a tip
   explaining the env var pattern for CI.

3. Weak next steps: the completion message now shows numbered steps, static
   analysis command, and a docs link — so users know exactly what to do next.

Also removes unnecessary 'import os' from generated conftest when a literal
URL is used (os is only imported when the url expr references os.environ).
Before this change, a missing alembic.ini produced the same error
message for every collected test (typically 7 times), each with a
double traceback due to pytest.fail() being called inside an except
block.

Changes:
- Add pytest_sessionstart hook that validates MRTConfig once and calls
  pytest.exit() on fatal config errors -- one clear message, no tests run
- Break exception chain in mrt fixture (capture error string before
  calling pytest.fail) to eliminate "During handling of the above
  exception" noise
- Simplify MRTConfigError message in MRTFixture.__init__: Django hint
  is now only shown via pytest_sessionstart, not duplicated
- Update test assertion to match revised error message text
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 81.25000% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pytest_mrt/plugin.py 53.33% 5 Missing and 2 partials ⚠️
pytest_mrt/commands/init.py 93.93% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@croc100 croc100 merged commit db4affb into main Jun 8, 2026
13 checks passed
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.

2 participants