# FAILED tests/test_state.py::test_reads_cache - KeyError: 'user:1'

(Passes with `pytest -p no:randomly`, fails with random order)

- **ID:** `python/pytest-random-order-flaky`
- **Domain:** python
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

pytest-randomly shuffles test order each run; a test depends on state created by another test (shared cache, module global, DB row), so it fails when it runs first.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (93% success)
   ```
   Make the test self-contained with a fixture that seeds the cache:
@pytest.fixture
def seeded_cache(cache):
    cache.set('user:1', {'name': 'alice'})
    return cache
def test_reads_cache(seeded_cache):
    assert seeded_cache.get('user:1')['name'] == 'alice'
   ```
2. **** (90% success)
   ```
   Or reset shared state between tests:
@pytest.fixture(autouse=True)
def clean_cache(cache):
    cache.flush_all()
    yield
    cache.flush_all()
   ```

## Dead Ends

- **** — pytest-randomly ignores order markers unless `-p no:randomly` is set; the shuffle still happens. (85% fail)
- **** — Makes the order deterministic but still hides the inter-test dependency; a different seed on CI breaks it. (70% fail)
