Loading...
Loading...
Diagnose and fix tests that pass in isolation but fail when run concurrently. Covers shared state isolation and resource conflicts. References condition-based-waiting for timing issues.
npx skill4agent add rileyhilliard/claude-essentials fixing-flaky-testsTest passes alone, fails with others?
│
├─ Same error every time → Shared state
│ └─ Database, globals, files, singletons
│
├─ Random/timing failures → Race condition
│ └─ Use `condition-based-waiting` skill
│
└─ Resource errors (port, file lock) → Resource conflict
└─ Need unique resources per test/worker| State Type | Isolation Pattern |
|---|---|
| Database | Transaction rollback, savepoints, worker-specific DBs |
| Global variables | Reset in |
| Singletons | Provide fresh instance per test |
| Module state | |
| Files | Unique paths per test, temp directories |
| Environment vars | Save/restore in setup/teardown |
# Python: Savepoint rollback - each test gets rolled back
@pytest.fixture
async def db_session(db_engine):
async with db_engine.connect() as conn:
await conn.begin()
await conn.begin_nested() # Savepoint
# ... yield session ...
await conn.rollback() # All changes vanish// Jest: Reset mocks between tests
beforeEach(() => {
jest.clearAllMocks()
jest.resetModules() // Clear module cache before test
})
afterEach(() => {
jest.restoreAllMocks() // Restore spied functions
})condition-based-waitingfindBy// Bad
await sleep(500)
// Good
await waitFor(() => expect(result).toBe('done'))# Python pytest-xdist: unique DB per worker
@pytest.fixture(scope="session")
def database_url(worker_id):
if worker_id == "master":
return "postgresql://localhost/test"
return f"postgresql://localhost/test_{worker_id}"// Jest/Node: dynamic port allocation
const server = app.listen(0) // OS assigns available port
const port = server.address().portimport tempfile
@pytest.fixture
def temp_dir():
with tempfile.TemporaryDirectory() as d:
yield d| Stack | Reference |
|---|---|
| Python (pytest, SQLAlchemy) | references/python.md |
| Jest / Testing Library | references/jest.md |
| Playwright E2E | references/playwright.md |
# Run the specific test many times
pytest tests/test_flaky.py -x --count=20
# Run with parallelism
pytest -n auto
# Jest equivalent
jest --runInBand # First verify serial works
jest # Then verify parallel works