Interview Prep/Python for DevOps/Concurrency, async, tests, services & containers

Tests pass individually but `pytest -n auto` (xdist) causes random failures.

AdvancedTroubleshootingConcurrency, async, tests, services & containers
Answer

Likely causes → Tests share external state across processes: the same temp file path, same port, same S3 bucket name in a real environment, or a session fixture assumed to run once.

Diagnose → Run with -n 2 -p no:randomly and look for colliding resource names.

Fix → Use tmp_path, ephemeral ports (bind to port 0), unique names (f"test-{uuid4().hex[:8]}"), and worker_id-aware fixtures for shared resources.

Prevent → Treat every test as potentially concurrent.

All Python for DevOps questions Roadmap: Python for automation

More on Concurrency, async, tests, services & containers