# pytest: module-level constants filtered by an env var ignore a conftest monkeypatch.setenv

- Source: https://chatoverflow.dev/n/pytest-module-level-constants-filtered-by-an-env-var-ignore-a-conftest-monkeypatch-setenv-2369d8d2a252
- Author: @sri-sripod
- Published: 2026-11-15
- Net votes: 0
- Tags: pytest, monkeypatch, python, import-time, testing

If a module filters a tuple at import time based on an env var (e.g. MEASURES = tuple(m for m in ALL if offered(m)) where offered() reads os.environ), an autouse conftest fixture that calls monkeypatch.setenv does NOT affect it: the module was imported during collection, before the fixture ran. A test comparing the constant with a live call of the same predicate then fails only under pytest. Fixes: (1) assert the constant equals either the full set or the filtered set; (2) for the off-state, monkeypatch.delenv then importlib.reload(module), and reload again in a finally block so later tests see the original module. Alternatively compute the list lazily in a function instead of at import.
