Abstract
Flaky tests—those that exhibit non-deterministic behavior by passing or failing on the same code—are a significant hurdle in Continuous Integration (CI) pipelines. While existing research has focused on detecting flakiness, identifying the underlying root cause remains a manual and time-consuming process. This paper introduces FlakyCat, a framework designed to categorize flaky tests into specific root-cause classes (e.g., Concurrency, Async Waits, Time) using few-shot learning. By combining CodeBERT embeddings with a Siamese network, FlakyCat achieves a weighted F1 score of 70% even with limited labeled data, providing a scalable path toward automated test repair.
Introduction
In modern software development, flaky tests (often called "Heisenbugs") undermine the reliability of automated testing. They provide false signals to developers, erode trust in test suites, and consume significant debugging resources. The primary challenge in addressing flakiness is not just detection, but diagnosis. Automated repair tools exist, but they are typically specialized for specific types of bugs. To utilize these tools effectively, developers first need to know why a test is flaky.
Categorizing flaky tests is difficult because labeled examples are scarce and the code patterns indicating flakiness can be subtle. Traditional machine learning classifiers often fail in this "data-starved" domain. FlakyCat addresses this by treating categorization as a similarity-learning problem rather than a standard classification task, allowing it to generalize from a small number of examples.
The FlakyCat Framework
FlakyCat utilizes a two-stage pipeline to transform raw test code into a categorical diagnosis.
1. Semantic Code Representation
Rather than relying on simple keyword matching or "test smells," FlakyCat employs CodeBERT, a transformer-based model pre-trained on a massive corpus of code and natural language. CodeBERT captures the semantic context of the test code, converting it into high-dimensional vectors (embeddings). This allows the model to understand the relationship between different operations—such as how a thread sleep relates to an asynchronous callback—beyond mere syntax.
2. Few-Shot Learning via Siamese Networks
To overcome the lack of large-scale labeled datasets, the researchers implemented a Siamese Network architecture. Instead of learning to map a test directly to a label, the network is trained using a Triplet Loss function. This training objective forces the model to project tests of the same category close together in a vector space while pushing tests from different categories further apart.
During inference, a new "query" test is compared against a small "support set" of known examples. The model assigns the category of the most similar examples in the embedding space. This few-shot learning (FSL) approach is significantly more robust to imbalanced and small datasets than traditional models like Random Forests or SVMs.
Experimental Methodology
Dataset
The study utilized a curated dataset of 343 flaky tests from open-source Java projects, sourced from the Illinois dataset and manual mining of GitHub commit messages. To improve model robustness, the authors applied mutation-based data augmentation (e.g., renaming variables), resulting in a total of 639 training instances. The evaluation focused on the four most prevalent flakiness categories:
- Async Waits: Improper handling of asynchronous calls.
- Unordered Collections: Assuming a specific order in non-deterministic structures like HashMaps.
- Concurrency: Race conditions and multi-threading issues.
- Time: Dependencies on system clocks or hard-coded timeouts.
Interpretability
To ensure the model's decisions are actionable, the authors integrated an interpretability technique inspired by delta debugging. By systematically removing statements from a test and measuring the impact on the model's confidence, FlakyCat can highlight the specific lines of code most likely responsible for the non-deterministic behavior.
Results and Discussion
FlakyCat achieved a weighted F1 score of 70% and an AUC of 0.78, significantly outperforming traditional baselines.
- Category Performance: The model performed exceptionally well on Unordered Collections (F1: 0.85) and Async Waits (F1: 0.74). However, Concurrency proved more challenging (F1: 0.46). This discrepancy is likely because concurrency issues often involve complex interactions with the System Under Test (SUT) that are not fully captured within the test method's source code alone.
- Interpretability Success: In time-related flakiness, the model correctly identified assertions involving dates or time comparisons as the influential factors in 85% of cases.
- Scalability: When expanded to six categories (including Network and Randomness), the F1 score decreased to 57%. This suggests that as the number of categories increases, the semantic boundaries between them (e.g., a network timeout vs. a general async wait) become increasingly blurred.
Conclusion and Future Work
FlakyCat demonstrates that few-shot learning combined with pre-trained language models is a viable strategy for the automated categorization of flaky tests. By identifying the root cause of flakiness, FlakyCat provides a necessary bridge to automated repair tools.
Future research will focus on incorporating the source code of the System Under Test (SUT) to better diagnose concurrency and environment-dependent issues. Additionally, the authors plan to expand the framework to support multiple programming languages and integrate it directly into CI/CD pipelines to provide real-time feedback to developers.
Summary updated