HomeBlogFlaky Tests: Prevention, Diagnosis, and Cure

Flaky Tests: Prevention, Diagnosis, and Cure




Flaky tests are software tests that exhibit inconsistent results passing at times and failing at others, without any changes made to the code or test environment. This unpredictability makes them a significant challenge in the software development process. They undermine the reliability of automated testing systems, causing developers to question the validity of test outcomes. Flaky tests can result from various factors, including timing issues, dependencies on external systems, or concurrency problems, where multiple tests compete for shared resources. The nondeterministic nature of these tests complicates debugging and can lead to overlooked bugs, reducing the overall quality of the software. Addressing test dependencies and test concurrency issues is essential to mitigate flakiness.

Common Causes of Flaky Tests

Poorly Written Tests

  1. A strong test should be deterministic, meaning its failure clearly indicates a regression or a specific issue. Ensuring test determinism is key to reliable software testing.
  2. If a test lacks necessary assumptions or cannot enforce the assumptions it makes, it will produce inconsistent results, contributing to flakiness. Properly defining test assumptions is crucial for test reliability.

Async Wait

  1. Tests often need the application to complete a request, which takes time.
  2. Utilizing sleep statements to pause test execution can lead to failures if the application requires more time than anticipated, making the test outcome unreliable. Instead, consider using appropriate test timeouts to handle such scenarios.

Test Order Dependency

  1. Tests should run independently and not rely on the execution order or shared resources like files, memory, or databases. Ensuring test isolation and managing test resources effectively can prevent such issues.
  2. Flakiness arises when tests do not manage their dependencies effectively, leading to failures if shared data is not handled in a predetermined order. Properly managing test dependencies and test execution order is essential to avoid such problems.


  1. Issues occur when assumptions about the execution order of operations by different threads are incorrect. Addressing race conditions, test synchronization, and test parallelism can help mitigate these issues.
  2. A test expecting a specific behavior can fail if multiple valid code behaviors exist, leading to nondeterministic outcomes. Managing test variability is crucial to ensure consistent and reliable test results.

How to Identify Flaky Tests

Test Retries

Identifying flaky tests often involves the strategy of test retries. In local environments, developers might find it beneficial to retry flaky tests to pinpoint transient errors. This can be automated through most Integrated Development Environments (IDEs) or test frameworks which provide options to automate these retries, smoothing over intermittent issues. In contrast, Continuous Integration (CI) environments require a more nuanced approach. If the CI platform supports it, using tools like flaky test dashboards can help in tracking and fixing these tests without masking the problem by repeated test passes. Effective test monitoring and detailed test logs can further aid in identifying and addressing these issues.

Automated Detection Tools

Various tools and plugins have been developed to detect flaky tests automatically. For instance,pytest-flakefinder and cargo-nextest Test automation tools allow tests to be run multiple times to identify nondeterministic behaviors. These tools can be configured to retry failed tests a certain number of times and provide detailed reports which help in identifying the flaky tests. Using these automated tools helps in maintaining the reliability of the test suite over time by ensuring that flaky tests are identified and addressed promptly.

Historical Analysis

Reviewing historical data of test executions is crucial in identifying patterns of flakiness. Tests that exhibit intermittent failures across different builds or environments are likely to be flaky. This method involves analyzing past test results to detect any inconsistencies that could indicate flaky behavior. By maintaining detailed test logs and employing software that can analyze these patterns, teams can effectively track down and address the root causes of flakiness in their test suites through comprehensive test analysis.

Best Practices to Reduce Flaky Tests

Improving Test Reliability

To enhance the reliability of tests, it’s crucial to focus on creating assessments that consistently measure the desired competencies. This involves ensuring high test quality and test stability.

  1. Developing Clear Test Items: Ensure each question is unambiguous, allowing testers to understand exactly what is being assessed.
  2. Using Sufficient Test Lengths:Longer tests tend to provide more reliable results by covering a broader range of material.
  3. Consistent Test Environments: Standardize the testing conditions to avoid variability that could affect the outcomes.
  4. Regular Item Analysis:Periodically review test questions to eliminate those that perform poorly or are misunderstood by testers. This process is part of ongoing test refactoring and test maintenance.

Utilizing Tools and Frameworks

Leveraging specialized tools and frameworks can significantly aid in detecting and managing flaky tests:

  1. Automated Retesting: Implement systems that automatically rerun failed tests to distinguish between flaky and genuinely failed tests. This approach leverages test automation and test reruns to improve reliability.
  2. Flakiness Detection Tools: Use plugins and tools designed to identify and analyze the patterns of test failures, helping to pinpoint unstable tests. Effective test monitoring, test reporting, and test analysis are key to this process.
  3. Hermetic Testing Environments: Adopt test setups that isolate tests from external dependencies, ensuring that the tests are not influenced by external factors. This practice enhances the effectiveness of the test setup and test isolation.

Continuous Monitoring and Refactoring

Ongoing vigilance is key to maintaining the health of the test suite:

  1. Routine Test Suite Reviews: Schedule regular examinations of test results to identify and address emergent issues promptly.
  2. Refactoring Tests: Continuously improve test design and implementation to enhance stability and predictability.
  3. Education and Awareness: Cultivate a culture that understands the impact of flaky tests and emphasizes the importance of reliable testing practices.

By adhering to these best practices, teams can reduce the occurrence of flaky tests, thereby improving the stability and reliability of their software development processes and enhancing test stability.


The fight against flaky tests is both ongoing and evolving, underscoring the need for continuous monitoring, refinement of test strategies, and adoption of new tools designed to identify and neutralize these erratic tests. By embracing the best practices outlined, including the development of clear test items, utilization of appropriate tools, and fostering an environment of continuous education and awareness, teams can fortify their testing suites against the instability introduced by flakiness.



Mehdi Shokoohi

Software Quality Engineer

Recent posts

Recent comments