ASTRA is a Python-based framework that leverages Large Language Models (LLMs) to automate the entire lifecycle of unit testing: from generation and evaluation to autonomous repair. It is designed to create robust test suites that achieve high mutation and coverage scores with minimal human intervention.
The primary goal of ASTRA is to address the high cost and time consumption associated with writing and maintaining unit tests. It uses the Google Gemini API to intelligently generate and fix pytest tests for a given Python source file, creating a self-healing system that adapts to code changes and test failures.
- Automated Test Generation: Creates comprehensive
pytesttest suites from a Python source file. - Autonomous Test Repair: If tests fail during mutation testing, ASTRA identifies the root cause, captures a dynamic execution trace, and uses an LLM to surgically repair the failing test function.
- Mutation Testing Integration: Uses
mutpyto run mutation analysis and provides a final mutation score as a key quality metric. - Coverage Analysis: Measures branch coverage of the generated tests using
coverage.py. - Surgical AST-Based Fixes: Replaces only the broken test function using Abstract Syntax Trees (AST), preserving the integrity of the rest of the test suite.
The framework operates in a sequential, automated workflow:
- Input: Takes a single Python source file as input.
- Test Generation: Calls the Gemini API with a sophisticated prompt to generate an initial set of
pytesttests. - Mutation Analysis: Executes
mutpyto run mutation testing against the generated tests. - Failure Detection: If
mutpyreports test failures, the framework enters a repair loop. - Repair Loop:
a. The name of the first failing test is extracted from the error log.
b. The single failing test is re-run to capture a precise execution trace.
c. The LLM is called with a specialized "fix" prompt, providing the source code, the broken test, the error traceback, and the execution trace.
d. The LLM returns a corrected version of the test function.
e. The old function is surgically replaced with the new one using AST manipulation.
f. The loop repeats by re-running
mutpy. - Success: Once all tests pass, the framework calculates and reports the final mutation score and branch coverage.
To replicate this project and run the tool, you must perform the following configuration steps:
-
Install Dependencies: Install all required packages using pip.
pip install -r requirements.txt
-
Set Your API Key: Open
mutation_analyzer.pyand replace the empty string in theGEMINI_API_KEYvariable with your own Google Gemini API key.# mutation_analyzer.py GEMINI_API_KEY = "YOUR_API_KEY_HERE"
-
Add Your Prompts: In
mutation_analyzer.py, paste your custom prompts into thePROMPT_GENERATE_TESTSandPROMPT_FIX_TESTSvariables. The placeholders inside the prompt strings must be kept intact. -
Update MutPy Path: The script contains a hardcoded path to the
mut.pyexecutable. You must change this to match the location of theScriptsfolder in your Python installation.# mutation_analyzer.py # Find this line and update the path mutpy_path = os.path.join('D:\\Software\\Python\\Scripts', 'mut.py')
- Place the Python file you want to test into the root directory of the project.
- Open
mutation_analyzer.pyand update theINPUT_FILEvariable to point to your target file.# mutation_analyzer.py INPUT_FILE = "path/to/your/file.py"
- Run the script from your terminal:
python mutation_analyzer.py
The tool will create a mutation_output directory containing the source file to be mutated and the generated/repaired test file.