Unit Test Generator
Automatically create comprehensive unit tests for your code, improving code quality and test coverage.
Key Features
-
Unit Test Generation: The feature analyzes the code changes and generates comprehensive unit tests, covering various scenarios and edge cases.
-
Test Coverage: The generated tests cover a wide range of scenarios, including edge cases, boundary conditions, and exception handling.
-
Test Execution: The tests can be executed locally or integrated into a CI/CD pipeline for automated testing.
-
Test Reporting: The feature provides detailed reports, including test results, coverage statistics, and any issues or failures encountered during execution.
-
Continuous Integration: The generated tests can be updated or regenerated as the codebase evolves, ensuring they remain aligned with the latest changes and requirements.
How it Works
- Input the source code or directory for which you want to generate unit tests.
- The Unit Test Generator leverages advanced language models to analyze the code and generate appropriate unit tests in a format compatible with popular testing frameworks.
- The generator supports multiple programming languages and can handle entire directories of code files.
You can find an example here (opens in a new tab)
Usage Guide
Here's a detailed step-by-step guide on how to use the Unit Test Generator:
-
Follow the initial setup guide here.
-
Create a new Python file and import the UnitTestGenerator module from Kaizen package :
from kaizen.generator.unit_test import UnitTestGenerator generator = UnitTestGenerator()
-
Provide the source code file or directory path for which you want to generate unit tests:
You can run it for one file at a time:
generator.generate_tests( file_path="path/to/your/file.py" )
Or for a directory of files:
generator.generate_tests_from_dir( dir_path="path/to/your/directory" )
-
Optionally, you can configure output path, verbosity, and critique settings.
... output_path="path/to/your/directory", enable_critique=True, verbose=True, max_critique=1 ...
-
Run the generator to create unit tests.
generator.run_tests()
-
Display the test results:
for file_path, result in test_results.items(): print(f"Results for {file_path}:") if "error" in result: print(f" Error: {result['error']}") else: print(f" Tests run: {result.get('tests_run', 'N/A')}") print(f" Failures: {result.get('failures', 'N/A')}") print(f" Errors: {result.get('errors', 'N/A')}") print()
-
Review and integrate the generated tests into your test suite.
Example
Here's a complete example to generate unit tests for a specific file:
from kaizen.generator.unit_test import UnitTestGenerator
# Create an instance of the generator
generator = UnitTestGenerator()
# Generate tests for a specific file
generator.generate_tests(
file_path="kaizen/helpers/output.py",
enable_critique=True,
verbose=True
)
# Run the generated tests
test_results = generator.run_tests()
# Display the test results
for file_path, result in test_results.items():
print(f"Results for {file_path}:")
if "error" in result:
print(f" Error: {result['error']}")
else:
print(f" Tests run: {result.get('tests_run', 'N/A')}")
print(f" Failures: {result.get('failures', 'N/A')}")
print(f" Errors: {result.get('errors', 'N/A')}")
print()
Supported Languages
- Python (.py)
- JavaScript (.js)
- TypeScript (.ts)
- React (.jsx, .tsx)
- Rust (.rs)
Benefits
- Increased Test Coverage
- Time Efficiency
- Consistency in Testing
- Early Bug Detection
- Support for Multiple Programming Languages
- Continuous Improvement through AI Critique
Limitations
- AI Limitations: May not cover all edge cases or complex scenarios.
- Human Oversight: Generated tests should be reviewed and potentially modified by developers.
- Language Support: Limited to the supported programming languages.