Context
The get_aws_config() function in env.py uses @lru_cache to avoid re-reading environment variables on every call. This works with make test but causes test failures when running in parallel (pytest -n auto).
In short:
pytest-xdist runs tests across multiple workers
lru_cache can cache config from one test's fixture setup before another test's monkeypatch.setenv has executed
- The
autouse clear_config_cache fixture in conftest.py clears the cache between tests
- Fixture ordering across parallel workers is not guaranteed
Tests pass reliably when run sequentially (pytest -x -v).
Implementation
Options to investigate:
- Use
pytest-xdist worker-scoped fixtures - ensure each worker gets its own config initialization with correct env vars before any tests run
- Lazy singleton pattern - replace
lru_cache with a simple module-level _config: AWSEnvConfig | None = None that gets set on first access and cleared explicitly in tests
- Accept sequential test execution — if parallel test speed is not a priority, document that tests must run sequentially and update CI accordingly
Recommended: Option 2 - simplest change, no new dependencies, explicit control over cache lifecycle in tests.
Context
The
get_aws_config()function inenv.pyuses@lru_cacheto avoid re-reading environment variables on every call. This works withmake testbut causes test failures when running in parallel (pytest -n auto).In short:
pytest-xdistruns tests across multiple workerslru_cachecan cache config from one test's fixture setup before another test'smonkeypatch.setenvhas executedautouseclear_config_cachefixture inconftest.pyclears the cache between testsTests pass reliably when run sequentially (
pytest -x -v).Implementation
Options to investigate:
pytest-xdistworker-scoped fixtures - ensure each worker gets its own config initialization with correct env vars before any tests runlru_cachewith a simple module-level_config: AWSEnvConfig | None = Nonethat gets set on first access and cleared explicitly in testsRecommended: Option 2 - simplest change, no new dependencies, explicit control over cache lifecycle in tests.