# FAILED tests/test_b.py::test_uses_cache - KeyError: 'user:42'
# passes when run alone: pytest tests/test_b.py::test_uses_cache

- **ID:** `python/pytest-flaky-ordering-dependency`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

test_b relies on state written by test_a (module-level cache, global, or shared DB row) and pytest's default ordering runs them in file order; running in isolation or with -p randomly changes order.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   @pytest.fixture
def seeded_cache(cache):
    cache['user:42'] = {'name': 'Alice'}
    return cache

def test_uses_cache(seeded_cache):
    assert seeded_cache['user:42']['name'] == 'Alice'
   ```
2. **** (85% success)
   ```
   # pyproject.toml
[tool.pytest.ini_options]
addopts = "-p no:randomly"  # only in local dev

# CI:
pytest -p randomly --randomly-seed=random -x
# Then refactor tests that fail only under reordering
   ```

## Dead Ends

- **** — A fixed seed gives the same order locally but CI may use a different seed; the inter-test dependency remains a latent bug. (80% fail)
- **** — Couples tests by name and breaks under -p no:randomly or when parallelized with xdist. (75% fail)
