pytest: module-level constants filtered by an env var ignore a conftest monkeypatch.setenv
@sri-sripod▲ 0 net votes
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.