Software development

Why does the same software test fail only sometimes?

Share article

Why does the same software test fail only sometimes?

Two identical testing devices on a warm grey workbench. One small light is already on, the other is still off.
Same setup. A different moment.AI-generated illustrative image

An automated test clicks “Save” and looks for the confirmation “Saved”. On the first run, it finds it; on the second, it reports a failure. Neither the test nor the code being tested has changed. Perhaps on the second run, saving had not yet finished when the test checked. A test that sometimes passes and sometimes fails while the code stays unchanged is called a flaky test.[1][2]

A look at the clock

After the click, the test pauses for a fixed 100 milliseconds and checks once. In run A, the confirmation appears at 80 milliseconds. It is already there when the test checks, so the test passes. In run B, it appears at 160 milliseconds. At the moment of the check it is missing, so the test fails. Shortly afterwards, the program still reports that it has saved.

If the point is whether the program confirms the action, the test can wait specifically for “Saved”. In a variant of run B, the test sets a deadline of 200 milliseconds. The confirmation again arrives at 160 milliseconds. The test then moves on and passes. If the confirmation is still missing when the deadline runs out, the test reports a failure. All times in this example are chosen arbitrarily.

When does the test check?

Schematic example with arbitrarily chosen time values. All three tracks use the same scale from 0 to 200 milliseconds.

● Confirmation appears│ Single check◇ Deadline

Run A

0 ms · Save triggered

  • At 80 ms, the confirmation appears.
  • At 100 ms, the test checks: confirmation present, passed.

✓ Passed

Run B

0 ms · Save triggered

  • At 100 ms, the test checks: confirmation missing, failure.
  • At 160 ms, the confirmation appears – after the failed check.

× Failure

Run B with waiting

0 ms · Save triggered · Waiting for the confirmation

  • At 160 ms, the confirmation appears: the waiting ends and the test passes.
  • 200 ms is the deadline. Without a confirmation, the test would then report a failure.

✓ Passed

Run B still fails a separate performance requirement of “confirmation within 100 ms”.

A 2014 study describes a similar case. A test waited two seconds for a response from a server. The developers replaced the fixed pause with waiting specifically for the response and put a limit on how long the entire test could take.[1]

If the test is meant to check a fixed performance requirement, that requirement’s time limit is still what decides the outcome. If the requirement in the example is “confirmation within 100 milliseconds”, run B fails to meet it.

When one test depends on another

Order can matter too. In another case from the study, one test wrote a file that several other tests needed. If one of the tests that read the file ran first, the file was missing. If each test prepares the data it needs itself, or the environment is reset appropriately, the dependence on order is reduced.[1]

What two studies counted

The 2014 study examined 201 selected changes in the version histories of 51 open-source projects. These changes had been identified as likely fixes for unreliable tests. Three common classifications were:[1]

Our own presentation of selected categories from Table 2 in [1]. The study examined 201 changes in total.
Cause according to the studyChanges examined
Checking too early while operations are still running74
Problems with tasks running at the same time32
Dependence on the order of the tests19

A study of Python projects ran tests repeatedly, both in the same order and in a randomly shuffled order. Of the 7,571 tests with fluctuating results, the team attributed 4,461 to test order, about 59 percent. For these tests, the team observed the fluctuation only when the order was randomly shuffled. The figure applies to the cases found in this study.[2]

What does a successful second run mean?

If the result changes when the test is repeated, the fluctuation becomes visible. The cause may lie in the test, in the program or in the environment.[1][2] In the save example, two points in time help: When did the test check, and when did the confirmation arrive? A log of the failed run can provide exactly this trail.

Sources

[1] Qingzhou Luo, Farah Hariri, Lamyaa Eloussi, Darko Marinov: An Empirical Analysis of Flaky Tests. FSE 2014, 643–653.

Open original source [1]

[2] Martin Gruber, Stephan Lukasczyk, Florian Kroiß, Gordon Fraser: An Empirical Study of Flaky Tests in Python. ICST 2021, 148–158.

Open original source [2]