directed-test-input-generator
Generate targeted test inputs to reach specific code paths and hard-to-reach behaviors in Python code. Use when: (1) Targeting uncovered branches or specific execution paths, (2) Need coverage-guided test generation, (3) Want to leverage LLM understanding of code semantics for meaningful test inputs, (4) Testing boundary conditions and edge cases systematically, (5) Combining symbolic reasoning with fuzzing. Provides path analysis, constraint solving, coverage-guided strategies, and LLM-driven semantic generation for comprehensive test input creation.
git clone --depth 1 https://github.com/ArabelaTso/Skills-4-SE /tmp/directed-test-input-generator && cp -r /tmp/directed-test-input-generator/skills/directed-test-input-generator ~/.claude/skills/directed-test-input-generatorSKILL.md
# Directed Test Input Generator
Generate test inputs that target specific code paths and hard-to-reach behaviors using program analysis, coverage feedback, and LLM-driven semantic understanding.
## Overview
Directed test input generation combines multiple techniques to create test inputs that explore specific execution paths:
1. **Path Analysis**: Extract control flow paths and their constraints
2. **Constraint Solving**: Generate inputs satisfying path conditions
3. **Coverage Guidance**: Use coverage feedback to iteratively reach new paths
4. **LLM Semantic Understanding**: Leverage code understanding for meaningful inputs
## Quick Start
### Basic Workflow
```python
# 1. Analyze code to extract paths
from scripts.path_analyzer import analyze_code_paths
paths = analyze_code_paths(source_code)
# 2. Generate inputs for each path
from scripts.input_generator import generate_test_suite
test_suite = generate_test_suite(paths)
# 3. Execute tests and measure coverage
for path_id, test_data in test_suite.items():
result = execute_test(function, test_data['inputs'])
verify_coverage(result, test_data['target_line'])
```
## Core Techniques
### 1. Path Analysis and Extraction
Extract execution paths and their constraints from code:
```python
from scripts.path_analyzer import analyze_code_paths, print_paths
source = """
def validate_age(age, country):
if age < 0:
raise ValueError("Invalid age")
if age < 18:
return "minor"
if age >= 65 and country == "US":
return "senior_us"
return "adult"
"""
paths = analyze_code_paths(source)
print_paths(paths)
# Output:
# Path #0: exception handler (ValueError) (line 3)
# Conditions:
# - age < 0
#
# Path #1: if branch (line 5)
# Conditions:
# - age >= 0
# - age < 18
#
# Path #2: if branch (line 7)
# Conditions:
# - age >= 0
# - age >= 18
# - age >= 65
# - country == US
```
### 2. Constraint-Based Input Generation
Generate inputs that satisfy specific path constraints:
```python
from scripts.input_generator import TestInputGenerator
generator = TestInputGenerator()
# Generate input for path: age >= 65 AND country == "US"
constraints = {
"age": ["age >= 65"],
"country": ["country == US"]
}
inputs = generator.generate_for_path(constraints)
# Result: {"age": 65, "country": "US"}
```
### 3. Edge Case Generation
Generate boundary values systematically:
```python
from scripts.input_generator import EdgeCaseGenerator
# Generate edge cases for integer type
edge_cases = EdgeCaseGenerator.generate_edge_cases(int)
# [0, -1, 1, -2147483648, 2147483647, ...]
# Generate boundary values around a threshold
boundaries = EdgeCaseGenerator.generate_boundary_values(">", 65)
# [64, 65, 66, 67] - values around the boundary
```
### 4. Coverage-Guided Generation
Iteratively generate inputs to maximize coverage:
```python
def coverage_guided_testing(function, max_iterations=100):
"""
Generate test inputs guided by coverage feedback.
"""
covered_lines = set()
test_corpus = []
# Start with initial inputs
current_inputs = generate_seed_inputs(function)
for i in range(max_iterations):
# Execute and measure coverage
new_coverage = execute_with_coverage(function, current_inputs)
if new_coverage - covered_lines:
# New coverage reached - save inputs
test_corpus.append(current_inputs)
covered_lines.update(new_coverage)
# Mutate inputs to explore new paths
current_inputs = mutate_toward_uncovered(current_inputs, covered_lines)
return test_corpus
```
See **[coverage_strategies.md](references/coverage_strategies.md)** for detailed coverage-guided strategies.
### 5. LLM-Driven Semantic Generation
Use LLM understanding of code semantics to generate meaningful inputs:
```python
# Example: LLM generates semantically appropriate inputs
function_code = """
def book_flight(passenger_age, departure_date, destination_country):
# ... booking logic
"""
# LLM understands parameter semantics and generates realistic inputs:
llm_generated = [
{
"passenger_age": 35, # Adult
"departure_date": "2026-06-15", # Future date
"destination_country": "US" # Valid country code
},
{
"passenger_age": 8, # Child passenger
"departure_date": "2026-07-20",
"destination_country": "UK"
},
{
"passenger_age": 72, # Senior citizen
"departure_date": "2026-08-10",
"destination_country": "CA"
}
]
```
See **[llm_patterns.md](references/llm_patterns.md)** for comprehensive LLM-driven generation patterns.
## Common Use Cases
### Use Case 1: Target Specific Branch
Generate input to reach an uncovered branch:
```python
# Target: age > 100 branch
def check_age(age):
if age > 100:
return "exceptionally_old" # Want to test this
return "normal"
# Analyze paths
paths = analyze_code_paths(check_age_source)
target_path = paths[0] # age > 100 path
# Generate input
generator = TestInputGenerator()
constraints = target_path.get_constraints()
test_input = generator.generate_for_path(constraints)
# Result: {"age": 101}
# Test
assert check_age(**test_input) == "exceptionally_old"
```
### Use Case 2: Systematic Boundary Testing
Test all boundaries in a function:
```python
def calculate_discount(age, is_premium):
if age < 18:
return 0.0
elif age < 65:
return 0.1 if is_premium else 0.05
else:
return 0.2 if is_premium else 0.15
# Generate boundary test suite
boundaries = [
{"age": 17, "is_premium": False}, # Just below 18
{"age": 18, "is_premium": False}, # Exactly 18
{"age": 19, "is_premium": True}, # Just above 18
{"age": 64, "is_premium": False}, # Just below 65
{"age": 65, "is_premium": True}, # Exactly 65
{"age": 66, "is_premium": False}, # Just above 65
]
for inputs in boundaries:Applies abstract interpretation using different abstract domains (intervals, octagons, polyhedra, sign, congruence) to statically analyze program variables and infer invariants, value ranges, and relationships. Use when analyzing program properties, inferring loop invariants, detecting potential errors, or understanding variable relationships through static analysis.
Uses abstract interpretation to automatically infer loop invariants, function preconditions, and postconditions for formal verification. Generates invariants that capture program behavior and support correctness proofs in Dafny, Isabelle, Coq, and other verification systems. Use when adding formal specifications to code, generating verification conditions, inferring contracts for functions, or discovering loop invariants for proofs.
Performs abstract interpretation over source code to infer possible program states, variable ranges, and data properties without executing the program. Reports potential runtime errors including out-of-bounds accesses, null dereferences, type inconsistencies, division by zero, and integer overflows. Use when analyzing code for potential runtime errors, performing static analysis, checking safety properties, or verifying program behavior without execution.
Performs abstract interpretation to produce summarized execution traces and high-level program behavior representations. Highlights key control flow paths, variable relationships, loop invariants, function summaries, and potential runtime states using abstract domains (intervals, signs, nullness, etc.). Use when analyzing program behavior, understanding execution paths, computing loop invariants, tracking variable ranges, detecting potential runtime errors, or generating program summaries without concrete execution.
Create ACSL (ANSI/ISO C Specification Language) formal annotations for C/C++ programs. Use this skill when working with formal verification, adding function contracts (requires/ensures), loop invariants, assertions, memory safety annotations, or any ACSL specifications. Supports Frama-C verification and generates comprehensive formal specifications for C/C++ code.
CLI-based browser automation with persistent page state using ref-based element interaction. Use when users ask to navigate websites, interact with web pages, fill forms, take screenshots, test web applications, or extract information from web pages.
Detects and analyzes ambiguous language in software requirements and user stories. Use when reviewing requirements documents, user stories, specifications, or any software requirement text to identify vague quantifiers, unclear scope, undefined terms, missing edge cases, subjective language, and incomplete specifications. Provides detailed analysis with clarifying questions and suggested improvements.
Design and review APIs with suggestions for endpoints, parameters, return types, and best practices. Use when designing new APIs from requirements, reviewing existing API designs, generating API documentation, or getting implementation guidance. Supports REST APIs with focus on endpoint structure, request/response schemas, authentication, pagination, filtering, versioning, and OpenAPI specifications. Triggers when users ask to design, review, document, or improve APIs.