Skip to content

Put the pytest plugin's container in test mode from the start - #333

Merged
antonrh merged 1 commit into
mainfrom
pytest-plugin-test-mode
Oct 4, 2026
Merged

antonrh merged 1 commit into
mainfrom
pytest-plugin-test-mode

Conversation

@antonrh

@antonrh antonrh commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

Problem

The plugin enabled test mode only when the container fixture was set up. A conftest that overrides through the imported container (with anydi_container set) never requests that fixture, so every override() warned with Using override() without enabling test mode until some test happened to take container. The warning count, and whether the override cache was active, depended on test order and filtering.

Changes

  • The container named by anydi_container is loaded in pytest_configure and put in test mode there. A bad path is a usage error naming the setting.
  • A global container created while the tests are collected enters test mode in pytest_collection_finish, before any fixture runs.
  • pytest_unconfigure turns test mode off on the containers the plugin enabled and restores the global container it replaced, so an in-process second run starts clean.
  • A project's own container fixture keeps working. Its container enters test mode when the fixture first runs, as before. The anydi_django fallback stays lazy.
  • New anydi ini flag, default true. With anydi = false the plugin registers no fixtures or hooks: no container fixture, no injection, test mode and the global container untouched.
  • Docs: "Test mode" and "Turning the plugin off" sections in docs/usage/testing.md.

Tests

New pytester tests: plugin disabled, overrides from a conftest with -W error::UserWarning in both test orders, a global container created at collection, an invalid anydi_container, cleanup after the session. All fail on the previous plugin. The plugin test run now also passes -p pytester (Makefile and CI).

Test mode was switched on only when the `container` fixture ran, so a
conftest overriding through the imported container warned on every
override until some test happened to request the fixture, and the number
of warnings depended on test order.

The container named by `anydi_container` now enters test mode in
`pytest_configure`, and a global container created while the tests are
collected enters it before the first fixture runs. Test mode is turned
off and the replaced global container restored at session end. A
container from a project's own `container` fixture still enters test
mode when the fixture first runs.

A new `anydi` ini flag, on by default, turns the plugin off when set to
false: no `container` fixture, no injection, the container left alone.
@codspeed

codspeed Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 17 untouched benchmarks


Comparing pytest-plugin-test-mode (3b90944) with main (2172c0c)

Open in CodSpeed

@antonrh
antonrh merged commit 35f5e66 into main Oct 4, 2026
10 checks passed
@antonrh
antonrh deleted the pytest-plugin-test-mode branch October 4, 2026 10:06
@antonrh antonrh mentioned this pull request Oct 4, 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.

1 participant