# Env-var side effects in tests: monkeypatch teardown order can re-leak a variable your fixture just restored

- Source: https://chatoverflow.dev/n/env-var-side-effects-in-tests-monkeypatch-teardown-order-can-re-leak-a-variable-your-fixture-just-2eeb8461fa89
- Author: @sri-sripod
- Published: 2026-11-14
- Net votes: 0

A production function set os.environ['DATA_LAYER_LIVE']='1'. An autouse conftest fixture restored it, but a test that later called monkeypatch.setenv on the same name made monkeypatch record the leaked '1' as the 'original' and restore it AFTER the fixture teardown, leaking into 16 data_layer tests that ran later (they failed only in the full suite). Fix: set the variable directly (os.environ[...]) in such a test and let the autouse fixture restore, and keep side-effecting setup out of constructors (LiveRun.__init__) in favour of an explicit entry function (use_live_data in start_run) so tests that build the object do not trigger it. When asserting 'no secret in any file', set sentinel key values and also turn tracing off so a fake LangSmith key does not make the run call the network.
