[Paper Review] exLong: Generating Exceptional Behavior Tests with Large Language Models
exLóng is a fine-tuned LLM based on CodeLlama that generates high-quality exceptional behavior tests (EBTs) by reasoning over execution traces, guard conditions, and similar non-EBTs. It outperforms state-of-the-art models (CAT-LM, GPT-3.5) and analysis-based tools (Randoop, EvoSuite), with 23 EBTs from exLóng already accepted in open-source projects.
Many popular programming languages, including C#, Java, and Python, support exceptions. Exceptions are thrown during program execution if an unwanted event happens, e.g., a method is invoked with an illegal argument value. Software developers write exceptional behavior tests (EBTs) to check that their code detects unwanted events and throws appropriate exceptions. Prior research studies have shown the importance of EBTs, but those studies also highlighted that developers put most of their efforts on "happy paths", e.g., paths without unwanted events. To help developers fill the gap, we present the first framework, dubbed exLong, that automatically generates EBTs. exLong is a large language model instruction fine-tuned from CodeLlama and embeds reasoning about traces that lead to throw statements, conditional expressions that guard throw statements, and non-exceptional behavior tests that execute similar traces. We compare exLong with the state-of-the-art models for test generation (CAT-LM) and one of the strongest foundation models (GPT-4o), as well as with analysis-based tools for test generation (Randoop and EvoSuite). Our results show that exLong outperforms existing models and tools. Furthermore, we contributed several pull requests to open-source projects and 23 EBTs generated by exLong were already accepted.
Motivation & Objective
- Address the critical gap in automated testing where developers under-test exceptional behaviors due to focus on 'happy paths'.
- Overcome the limitation of existing LLMs and tools in generating meaningful, readable, and effective exceptional behavior tests (EBTs).
- Provide a targeted, instruction-tuned LLM framework that generates EBTs for specific throw statements in code.
- Improve code quality assurance by automating the generation of high-coverage, semantically meaningful EBTs.
- Demonstrate practical impact by contributing accepted EBTs to real open-source projects.
Proposed method
- Fine-tune CodeLlama using instruction tuning with a novel task instruction designed to embed reasoning about traces leading to target throw statements.
- Incorporate guard expressions (conditional logic) that control when a throw statement is executed, ensuring semantic correctness.
- Integrate context from non-exceptional behavior tests (non-EBTs) that execute similar code paths to improve test realism and structure.
- Use stack traces from non-EBTs to guide the construction of test setup and invocation logic in the generated EBTs.
- Structure the input prompt to include method signature, target throw statement, relevant guard conditions, and example non-EBTs for context.
- Generate EBTs that include proper test setup (e.g., input creation), method invocation, and expected exception assertion using JUnit-style annotations.

Experimental results
Research questions
- RQ1Can an instruction-tuned LLM generate exceptional behavior tests (EBTs) that are semantically correct and executable?
- RQ2How does exLóng compare to state-of-the-art LLM-based test generators (e.g., CAT-LM) and foundation models (e.g., GPT-3.5) in generating EBTs?
- RQ3Can exLóng outperform analysis-based test generation tools (e.g., Randoop, EvoSuite) in generating EBTs for specific throw statements?
- RQ4To what extent can exLóng generate EBTs that are accepted in real-world open-source projects?
- RQ5Does incorporating reasoning about execution traces, guard conditions, and similar non-EBTs improve EBT quality and coverage?
Key findings
- exLóng outperforms CAT-LM and GPT-3.5 in generating executable and correct EBTs, particularly in targeting specific throw statements.
- exLóng generates EBTs with higher readability and structural correctness compared to analysis-based tools like Randoop and EvoSuite.
- exLóng achieves higher throw statement coverage than baseline tools, demonstrating effective targeting of exceptional behaviors.
- 23 EBTs generated by exLóng were accepted into open-source projects, validating real-world utility and code quality impact.
- The integration of execution traces, guard expressions, and non-EBT context significantly improves the quality and correctness of generated EBTs.
- exLóng enables a developer-oriented use case by allowing targeted EBT generation for specific methods and throw statements, unlike whole-program random or search-based tools.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.