Causes of flaky tests

Flaky Tests

Sep 27, 2023

In the dynamic realm of automated testing, flaky tests stand as an permanent challenge that has perplexed developers and quality assurance experts alike. These tests, notorious for their unpredictable outcomes, pose a significant hurdle to the reliability and efficiency of the testing process. To actually address the issue, it becomes imperative to uncover the root causes that underlie this mysterious phenomenon.

Defining Flaky Tests: A Closer Look at the Conundrum

Flaky tests, often termed as non-deterministic tests, exhibit inconsistency in their behavior across different test runs. The same failed test you saw in previous runs could pass the next time These outcomes can wreak havoc on the sanity of developers and the stability of the codebase. This erratic behavior is akin to a mirage in the testing desert, leading teams to question the necessity of their testing suite.

Common Causes of Flakiness: Navigating the Terrain

  1. Concurrency and Parallelism: In a world where tests are executed concurrently to expedite the testing process, race conditions can stealthily creep in, causing tests to interact in unforeseen ways. Shared resources and improper synchronization mechanisms can lead to unexpected outcomes.

  2. Dependency on External Factors: Flaky tests often result from external dependencies like APIs or databases that are beyond the control of the test environment. Network delays, changes in data, or API alterations can all contribute to inconsistent test results.

  3. Timing and Asynchronicity: Tests that involve asynchronous operations can fall prey to timing issues, leading to varying outcomes based on the speed of execution. Timeouts that are too short or too long can equally trigger flakiness.

  4. Front-End and UI Testing: The dynamic nature of front-end development can introduce flakiness due to changes in the DOM, rendering discrepancies, or browser-specific behaviors.

  5. Test Data Management: Flakiness can emerge when tests share or modify the same test data, causing unexpected interactions that produce inconsistent results.

  6. Concurrency Plugins and Frameworks: While concurrency frameworks aim to enhance test execution speed, they can also introduce flakiness if not handled meticulously.

Unearthing Solutions: Taming the Flakiness Beast

  1. Rigorous Debugging and Reruns: When flaky tests strike, a thorough debugging process is vital. Analyze logs, test reports, and test data to identify patterns. Sometimes, simply rerunning the test suite can yield different outcomes, helping narrow down the root cause.

  2. Isolation and Determinism: Design tests to be isolated and deterministic, minimizing dependencies on external factors or shared resources. Mocking and stubbing can aid in achieving this.

  3. Retries and Thresholds: Incorporate retry mechanisms in your test suite to mitigate transient failures. However, tread cautiously, as excessive retries can extend test execution times.

  4. Continuous Monitoring: Integrate test reports and metrics into your workflow to keep a vigilant eye on test flakiness trends. Continuous integration tools like GitHub Actions can help in automating this process.

The Path Forward: A Reliable Testing Landscape

While flaky tests might appear as insurmountable challenges, dedicated efforts in understanding and mitigating their underlying causes can pave the way to more reliable tests. One simple solution is to use BuildPulse, which find and flaky tests, reports on impact, and quarantines them - an end to end solution.

FAQ

What is the difference between a flaky test and a false positive?

A false positive is a test failure in your test suite due to an actual error in the code being executed, or a mismatch in what the test expects from the code.

A flaky test is when you have conflicting test results for the same code. For example, while running tests if you see that a test fails and passes, but the code hasn’t changed, then it’s a flaky test. There’s many causes of flakiness.

What is an example of a flaky test?

An example can be seen in growing test suites - when pull request builds fail for changes you haven’t made. Put differently, when you see a test pass and fail without any code change. These failed tests are flaky tests.

What are common causes of flakiness?

Broken assumptions in test automation and development process can introduce flaky tests - for example, if test data is shared between different tests whether asynchronous, high concurrency, or sequential, the results of one test can affect another. 

Poorly written test code can also be a factor. Improper polling, race conditions, improper event dependency handling, shared test data, or timeout handling for network requests or page loads. Any of these can lead to flaky test failures and test flakiness.

End-to-end tests that rely on internal API uptime can cause test flakiness and test failures.

What's the impact of flaky tests?

Flaky tests can wreck havoc on the development process - from wasted developer time from test retries, to creating bugs and product instability and missed releases, time-consuming flaky tests can grind your development process to a halt.

What is the best way to resolve or fix flaky tests?

Devops, software engineering, and software development teams will often need to compare code changes, logs, and other context across test environments from before the test instability started, and after - adding retries or reruns can also help with debugging. Test detection and test execution tooling can help automate this process as well. 

BuildPulse enables you to find, assess impact metrics, quarantine, and fix flaky tests.

What are some strategies for preventing flaky tests?

Paying attention and prioritizing flaky tests as they come up can be a good way to prevent them from becoming an issue. This is where a testing culture is important - if a flaky test case is spotted by an engineer, it should be logged right away. This, however, takes a certain level of hygiene - BuildPulse can provide monitoring so flaky tests are caught right away.

What type of tests have flaky tests?

Flaky tests can be seen across the testing process - unit tests, integration tests, end-to-end tests, UI tests, acceptance tests.

What if I don't have that many flaky tests?

Flaky tests can be stealthy - often ignored by engineers and test runs are retried, they build up until they can’t be ignored anymore. These automated tests slow down developer productivity, impact functionality, and reduce confidence in test results and test suites. Better to get ahead while it’s easy and invest in test management.

It’s also important to prevent regressions to catch flakiness early while it’s manageable.

What languages and continuous integration providers does BuildPulse work with?

BuildPulse integrates with all continuous integration providers (including GitHub Actions, BitBucket Pipelines, and more), test frameworks, and workflows.

Combat non-determinism, drive test confidence, and provide the best experience you can to your developers!

How long does implementation/integration with BuildPulse take?

Implementation/integration takes 5 minutes!

Ready for Takeoff?

Ready for Takeoff?

Ready for Takeoff?