# FAILED tests/test_state.py::test_second_run - AssertionError: assert 2 == 1
# passes when run alone, fails when run after tests/test_state.py::test_first_run

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

## Root Cause

Test isolation is broken: a module-level mutable (cache, global, class attribute, or DB row) is mutated by an earlier test and not reset. Pytest's default collection order exposes the leak.

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   @pytest.fixture(autouse=True)
def reset_cache():
    from myapp import cache
    cache.clear()
    yield
    cache.clear()
   ```
2. **** (85% success)
   ```
   pip install pytest-forked
pytest --forked tests/test_state.py
   ```
3. **** (90% success)
   ```
   pytest --stepwise tests/test_state.py
# or:
pytest tests/test_state.py::test_first_run tests/test_state.py::test_second_run -v
   ```

## Dead Ends

- **** — Shuffling changes which test fails, not whether tests are isolated; flakiness moves around instead of disappearing. (80% fail)
- **** — Reruns the same broken state; if the first run mutated global state, retries still see the mutated value. (85% fail)
