TL;DR
This paper introduces MSeqGen, a novel approach to automatically generate unit tests for object-oriented code by mining existing codebases for method call sequences. By learning from how developers actually use code, MSeqGen significantly boosts the structural coverage achieved by existing test generation tools like Randoop and Pex, particularly for hard-to-reach branches.
What Problem Does This Paper Address?
Achieving high structural coverage (like branch coverage) in unit testing is crucial for ensuring code quality. For object-oriented (OO) code, this often requires generating complex sequences of method calls that create and manipulate objects to reach specific target states within the code under test. However, the sheer number of possible sequences makes automatic generation a significant challenge. Existing approaches like random testing and dynamic symbolic execution (DSE) struggle to generate these necessary sequences, leading to incomplete test suites and missed bugs. This paper aims to bridge this gap by leveraging existing code usage patterns to generate more effective test sequences.
Methods
MSeqGen employs a multi-stage approach:
- Code Searching: To handle large codebases efficiently, MSeqGen uses keyword searches based on target classes (classes of interest for test generation) to identify relevant method bodies. This avoids analyzing entire codebases.
- Code Analysis: It constructs control-flow graphs (CFGs) for the identified method bodies and marks "nodes of interest" that produce objects of target classes. Irrelevant method bodies are filtered out if their CFGs lack these nodes.
- Sequence Extraction: Sequences are extracted by traversing the CFGs from nodes of interest to the end. This process identifies method calls that create and mutate objects of target classes.
- Sequence Generalization: To make extracted sequences more adaptable, MSeqGen converts concrete values for primitive types into symbolic values, creating "skeletons." This allows DSE-based tools to explore a wider range of concrete values that satisfy constraints.
- Generation of New Sequences: If individual extracted sequences are insufficient, MSeqGen randomly combines them to create new, potentially more effective sequences.
MSeqGen then uses these extracted and generalized sequences to assist existing test generation tools. For random testing (like Randoop), it synthesizes method bodies that return objects created by the mined sequences. For DSE-based approaches (like Pex), it converts sequences into "factory methods" that can be used by the DSE tool.
Datasets
The paper evaluates MSeqGen on two real-world .NET applications:
- QuickGraph: A C# graph library (approx. 5 KLOC).
- Facebook: A Facebook developer toolkit (approx. 40 KLOC).
The evaluation uses the codebases of these applications, including their associated test code, as the source for mining sequences. No new benchmarks were introduced; instead, existing applications were used to demonstrate the approach's scalability and effectiveness.
Key Insights and Ideas
- Leveraging Existing Usage: The core insight is that how developers actually use code in practice (captured in existing codebases) provides valuable hints for generating effective test sequences, especially for complex OO interactions.
- Bridging the Gap for Hard-to-Cover Branches: MSeqGen's sequences are particularly effective at improving coverage for branches that are difficult for standard random or DSE approaches to reach, suggesting that these branches often require specific object states that are naturally formed in real-world usage.
- Sequence Generalization is Key for DSE: Converting concrete sequences into symbolic skeletons is crucial for enabling DSE tools to explore the state space more effectively and find values that satisfy complex constraints.
- Factory Methods for DSE Integration: The use of "factory methods" provides a practical mechanism to integrate mined sequences into DSE tools like Pex, allowing them to leverage pre-discovered object creation patterns.
Key Results
MSeqGen significantly improved branch coverage when used to assist existing test generation tools:
- With Randoop (Random Testing): Achieved an average of 8.7% higher branch coverage, with a maximum increase of 20.0% in one namespace.
- With Pex (DSE-based): Achieved an average of 17.4% higher branch coverage, with a maximum increase of 22.5% in one namespace.
These improvements are notable because they target branches that were previously difficult to cover. The approach also demonstrated scalability, handling large codebases (up to 40 KLOC) within reasonable timeframes (around 5 minutes for analysis).
Limitations and Future Work
- Limitations of Pex Integration: The paper notes that Pex's current inability to automatically handle interfaces and their implementing classes limits the effectiveness of MSeqGen's factory methods in some scenarios.
- Difficult Branches: Even with MSeqGen, some branches remain uncovered, often due to extremely specific argument object states or complex control flow that requires more than just object creation sequences (e.g., specific graph structures with outgoing edges).
- Future Work:
- Address Pex's interface handling limitations.
- Explore evolutionary approaches to generate new sequences, potentially covering less common usage patterns.
- Support more advanced language features like inheritance and generics, which are currently not fully handled.
- Automate the fixing of compilation errors for extracted sequences.
Summary updated