Why should tests not depend on execution order?
Options
- Test order is always alphabetical, so it is safe
- Order can change; hidden dependencies cause failures (Correct option)
- It makes the tests run slower than they should
- pytest forbids tests that rely on any order
Answer
Order can change (parallel runs, random ordering, running one test alone); hidden dependencies cause failures that are hard to reproduce
Why
If test B only passes because test A created some data first, then B fails when run alone, in parallel or in a different order.
Each test should create what it needs and clean up after itself.
Plugins like pytest-randomly shuffle the order to reveal hidden dependencies; pytest-xdist runs tests in parallel.
Remember
Every test must work alone, in any order.