How to fix flaky tests

Aug 30, 2023

Software development is a complex process that involves writing and testing code to ensure its functionality. Flaky tests, however, can be a real roadblock in this development process. They can cause a lot of time-consuming reruns and debugging, as they produce inconsistent test results. This can result in failed tests, making it challenging for a software engineer to maintain an efficient workflow. In this guide, we'll discuss how to find and fix flaky tests in your test suite.

The Problem with Flaky Tests

Flaky tests are those that exhibit non-deterministic behavior. These are tests that yield different outcomes even when there are no changes in the code or test environment. They are problematic because they introduce uncertainty into the test results, which can interfere with the continuous integration (CI) and CD pipeline workflows. They can cause unnecessary test failures, leading to time-consuming debugging and rerunning of tests. Test flakiness can be introduced by various dependencies and other factors like concurrency, asynchronous code, and order dependency among others.

Step 1: Commit to Fixing the Problem Right Away!

The first step to deal with flaky tests is to commit to fixing the issue promptly. When a test fails, it might be tempting to simply rerun it until it passes. However, this approach doesn't address the root cause of the flakiness and can lead to more failed tests down the line. It is essential to prioritize identifying and fixing flaky tests to maintain the reliability of your test suite and to ensure the accuracy of test results.

Step 2: Find the Flaky Tests in Your Suite

The next step is identifying the flaky tests in your test suite. BuildPulse offers a comprehensive platform to detect flaky tests, measure their impact on the product and the engineering team, and alert stakeholders of any issues while they’re still manageable.

You can also consider some other techniques:

Saving Debugging Information

Every time a test fails, make sure to save all relevant debugging information. This includes logs, screenshots, and any other data that could help diagnose the issue. This data can be invaluable when trying to find the root cause of the flakiness.

SSH Debugging

Some testing platforms allow for SSH debugging, which can provide a live look into the test environment when a test fails. This can help uncover hidden causes of flakiness.

A Picture Says More Than a Thousand Words

Whenever possible, use visual aids like screenshots and screen recordings. These can help illustrate what's happening at the frontend when a test fails.

Configuring Reports in the Pipeline

Configure your CI pipeline to generate detailed test reports whenever a test run completes. These reports can help identify patterns and trends related to test flakiness. Integration with platforms like GitHub can further streamline this process.

Step 3: Documenting Flaky Tests

Keeping a detailed record of flaky tests is critical. Document every instance of a flaky test, including when it occurred, the test environment, any error messages, and any attempted fixes. This documentation can be helpful for future reference and can assist in identifying common causes of flakiness.

Step 4: Determining the Cause and Fixing the Test

Once you've identified and documented the flaky tests, the next step is to determine the cause and fix the issue. Several common causes can lead to test flakiness:

Environmental Differences

Flakiness can often be attributed to differences in test environments. Make sure that the test environment closely mirrors the production environment to reduce the risk of environmental flakiness.

Non-deterministic Code

If your code relies on random factors or specific timing, it can lead to flaky tests. Try to avoid non-deterministic code whenever possible.

Asynchronous Wait

Flaky tests can occur if your test cases do not properly handle asynchronous API calls. Implementing appropriate wait times or callbacks can help alleviate this issue.


Concurrency issues can also cause flakiness. If your tests are not properly synchronized, they can interfere with each other and lead to flaky outcomes.

Order Dependency

Tests should be independent and not rely on the order in which they are run. If test order is causing flakiness, consider refactoring your tests to eliminate these dependencies.

Improper Assumptions

Sometimes, tests make improper assumptions about the state of the system, leading to flaky outcomes. Review your test cases to ensure they accurately represent the expected functionality.

Fixing the Test

Once the root cause of the flakiness is determined, the final step is to fix the test. This can involve refactoring the test code, adjusting timeouts, changing the order of test execution, and so on. Utilize your testing framework and implement best practices for writing reliable test code.


Flaky tests can pose a significant challenge in software development, leading to inefficient workflows and unreliable test results. However, by committing to addressing the issue, identifying and documenting flaky tests, determining the cause, and implementing necessary changes, flaky tests can be effectively managed. Leveraging tools for test detection and reporting, like Slack notifications for test failures, can help maintain a reliable test suite, ensuring the quality of your end-to-end testing, unit tests, and regression testing. Remember that maintaining test reliability is a continuous process and plays a crucial role in successful software development.