What is test isolation?

Answer

Test isolation means each test runs independently without being affected by or affecting other tests. Violations of isolation: (1) Shared state: a test modifies a global variable or database record that another test depends on. (2) Test order dependency: tests pass only when run in a specific order (Test A must run before Test B). (3) Resource sharing: tests share a file or network socket without proper cleanup. (4) Time dependencies: tests that use DateTime.Now without injection (different results at different times). How to achieve isolation: (1) Reset state: use SetUp/TearDown to restore initial state. (2) Mock dependencies: replace external dependencies with controlled mocks. (3) Use transactions: wrap database tests in a transaction and roll back after each test. (4) Inject time: use IClock interface instead of static DateTime.Now. (5) Parallel safety: ensure tests can run concurrently. Isolated tests are reliable, order-independent, and diagnosable.