A test fails. Before changing anything, establish which of two things is true: the code is broken, or the test is wrong. Changing the test to pass is the most common response and the one that removes the value of having tests at all.
How do you tell which?
By checking what the test asserts against what should happen, independently of what the code currently does. If the expectation is correct, the code is wrong. If not, the test was encoding a mistake.
Generated tests are particularly prone to the second, since they are frequently written by reading the implementation.
What should you never do?
Adjust the expected value until it passes. That converts a test into a record of current behaviour, which detects nothing, and it is exactly what happens under time pressure.
What is the request?
Here is the failing test, the error, and the function it tests. First tell me whether the assertion is correct given what this function is supposed to do. Only then tell me what might be wrong.
Why do tests fail intermittently?
- Shared state. One test leaving something behind for another.
- Order dependence. Tests passing only in a particular sequence.
- Time. Dates, timeouts and anything using the current moment.
- External services. Real network calls in a test.
- Concurrency. Tests running in parallel touching the same thing.
How do you diagnose an unreliable test?
Run it alone. If it passes in isolation and fails in the suite, the cause is interaction with another test, which is almost always shared state that is not being reset.
Running the suite in a different order confirms it.
Why are unreliable tests worse than no tests?
Because they train people to ignore failures. A suite that fails occasionally for no reason produces a habit of rerunning until it passes, and that habit applies equally to a genuine failure.
Fixing or removing them is more valuable than adding new ones.
What about a test that passes and should not?
Worth checking when a bug reaches production despite coverage. A test that would pass with the function body removed asserts nothing, and generated suites contain these regularly.
What should the failure tell you?
What was expected, what was received and in which case. An assertion producing only a comparison failure is considerably less useful than one naming the scenario, and improving the message is worth doing while you are there.
What about tests that fail only in the pipeline?
Usually an environment difference rather than a test problem. Different versions, missing variables, a different time zone, or services available locally and not in the runner all produce this, and the test itself is frequently correct.
The fastest diagnosis is running the suite locally in a way that matches the pipeline as closely as possible, since a difference you can reproduce is a difference you can investigate. Time zone problems are worth suspecting early, because development machines and build runners are frequently configured differently and date handling is sensitive to it.
What is the order?
Establish whether the test is right, then fix the code, then confirm the test now fails without the fix. That last step is the one that proves the test was actually detecting something.