Sobes.tech
Middle

What is a Flaky Test and how to deal with it?

sobes.tech AI

Answer from AI

Flaky Test is an unstable test that sometimes passes and sometimes fails without any changes in the code or test environment. Such tests hinder automation and reduce confidence in test coverage.

The causes of flaky tests can vary: synchronization issues, dependence on external services, environment state, data races, timing issues, etc.

How to fight:

  • Isolate tests so they do not depend on each other.
  • Use explicit waits instead of fixed delays.
  • Minimize dependencies on external services, use mocks.
  • Analyze logs and rerun tests to identify causes of instability.
  • Rewrite or delete tests that are constantly flaky and do not provide value.

Example of waiting in Selenium on Java:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));