AI-Driven Automated Test Generation System

Yash Oza

🎯 Project Overview

Revolutionary approach to automated test generation using AI agents
Current automated test generation tools focus primarily on code coverage and lack:
Business logic understanding
Security vulnerability testing
Semantic context awareness
Multi-layered validation
An agentic AI system that combines multiple specialized agents to generate comprehensive test suites:
Multi-agent architecture
LLM-powered semantic analysis

🏗️ Multi-Agent Architecture

Specialized agents working together for comprehensive test generation
Main coordinator that orchestrates all agents and manages the overall workflow
Agent coordination
Workflow management
Result aggregation
Quality metrics tracking
AST-based function extraction
AI-powered scenario generation
Realistic test data creation
Framework-specific code generation
Edge case identification
Comprehensive capabilities for modern test generation
Test Type Description Use Case UNIT Basic unit tests All functions INTEGRATION Integration tests Functions with dependencies EDGE_CASE Boundary condition tests Complex functions SECURITY Security-focused tests Security-sensitive functions PERFORMANCE Performance tests Performance-critical functions ERROR_HANDLING Exception handling tests Functions with error handling
pytest (recommended)
unittest (built-in)
Custom framework adapters
CRITICAL: Security-sensitive, high-risk functions
HIGH: Complex functions with dependencies

🛠️ Installation & Setup

Get started with the agentic AI test generation system
Prerequisites: Python 3.7+ is required for this system to work properly.

📦 Required Dependencies

google-generativeai>=0.3.0
python-dotenv>=0.19.0
pytest>=7.0.0
nose2>=0.10.0
ast>=3.8

🔧 Installation Steps

# Clone the repository
git clone https://github.com/your-repo/agentic-ai-test-generation.git
cd agentic-ai-test-generation

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
cp .env.example .env
# Edit .env with your API keys

🔑 API Configuration

# Google Gemini API Key (required for AI features)
GEMINI_API_KEY=your_api_key_here

# Optional: Other configuration
DEFAULT_FRAMEWORK=pytest
DEFAULT_COVERAGE_TARGET=0.85
Note: You'll need a Google Gemini API key for AI-powered features. The system will work without it but with reduced semantic understanding capabilities.
Learn how to generate intelligent test suites

💻 Command Line Interface

# Generate tests for a single file
python main.py calculator.py --output-dir generated_tests

# Generate tests for multiple files
python main.py file1.py file2.py --output-dir tests

# Analyze entire directory
python main.py src/ --output-dir tests --framework pytest
# Set coverage target
python main.py src/ --coverage-target 0.95

# Specify test framework
python main.py src/ --framework unittest

# Use specific API key
python main.py src/ --api-key YOUR_GEMINI_API_KEY

# Security-focused analysis
python main.py auth_module.py --security-focus

# Performance test generation
python main.py data_processor.py --include-performance
from main import TestSuiteOrchestrator
from enums import TestFramework

# Configure the system
orchestrator = TestSuiteOrchestrator(
api_key="YOUR_GEMINI_API_KEY",
framework=TestFramework.PYTEST,
coverage_target=0.85
)

# Generate tests for specific files
results = orchestrator.generate_test_suite(
source_files=["calculator.py", "utils.py"],
output_dir="generated_tests"
)

# Process results
for result in results:
print(f"Generated {len(result.test_cases)} tests for {result.source_file}")
print(f"Coverage: {result.coverage_percentage}%")
print(f"Quality Score: {result.quality_score}")

# Advanced configuration
config = {
"coverage_target": 0.90,
"max_tests_per_function": 10,
"security_focus": True,
"include_performance_tests": True,
"framework": "pytest",
"output_format": "detailed",
"validation_strictness": "high"
}

orchestrator = TestSuiteOrchestrator(
api_key="YOUR_API_KEY",
**config
)
Complete API documentation for all system components
class TestSuiteOrchestrator:
def __init__(self, api_key: str, framework: TestFramework = TestFramework.PYTEST)

def generate_test_suite(self, source_files: List[str], output_dir: str) -> List[TestResult]

def set_coverage_target(self, target: float)

def enable_security_focus(self, enabled: bool = True)

def get_generation_report(self) -> GenerationReport
Class Description Key Properties FunctionInfo Function metadata and analysis name, parameters, return_type, complexity TestCase Generated test case representation name, type, code, priority, quality_score TestResult Test generation results source_file, test_cases, coverage, metrics GenerationReport Comprehensive generation summary total_tests, coverage_achieved, quality_metrics

💡 Real-World Examples

Practical examples demonstrating the system's capabilities

📊 Example 1: E-commerce Discount Calculator

def calculate_discount(price, discount_rate, user_type='regular'):
"""Calculate discount based on price, rate, and user type"""
if price < 0:
raise ValueError("Price cannot be negative")

if not 0 <= discount_rate <= 1:
raise ValueError("Discount rate must be between 0 and 1")

base_discount = price * discount_rate

# Premium users get additional 5% discount
if user_type == 'premium':
base_discount *= 1.05

# Maximum discount cap
max_discount = price * 0.5
return min(base_discount, max_discount)
import pytest
from calculator import calculate_discount

class TestCalculateDiscount:
"""AI-generated comprehensive test suite for calculate_discount function"""

def test_basic_discount_calculation(self):
"""Test basic discount calculation for regular users"""
result = calculate_discount(100, 0.1, 'regular')
assert result == 10.0

def test_premium_user_additional_discount(self):
"""Test premium users receive additional discount"""
regular_discount = calculate_discount(100, 0.1, 'regular')
premium_discount = calculate_discount(100, 0.1, 'premium')
assert premium_discount > regular_discount
assert premium_discount == 10.5

def test_maximum_discount_cap(self):
"""Test maximum discount cap is enforced"""
result = calculate_discount(100, 0.8, 'regular')
assert result == 50.0 # Capped at 50% of price

def test_negative_price_raises_error(self):
"""Test that negative prices raise ValueError"""
with pytest.raises(ValueError, match="Price cannot be negative"):
calculate_discount(-10, 0.1)

def test_invalid_discount_rate_raises_error(self):
"""Test that invalid discount rates raise ValueError"""
with pytest.raises(ValueError, match="Discount rate must be between 0 and 1"):
calculate_discount(100, 1.5)

with pytest.raises(ValueError, match="Discount rate must be between 0 and 1"):
calculate_discount(100, -0.1)

def test_zero_discount_rate(self):
"""Test edge case with zero discount rate"""
result = calculate_discount(100, 0)
assert result == 0

def test_maximum_discount_rate(self):
"""Test edge case with maximum discount rate"""
result = calculate_discount(100, 1.0)
assert result == 50.0 # Capped at 50%

def test_floating_point_precision(self):
"""Test floating point calculations are handled correctly"""
result = calculate_discount(99.99, 0.333)
assert abs(result - 33.33) < 0.01

🔒 Example 2: Security-Focused Authentication

import hashlib
import secrets

def authenticate_user(username, password, stored_hash, salt):
"""Authenticate user with secure password hashing"""
if not username or not password:
return False

# Hash the provided password with salt
password_hash = hashlib.pbkdf2_hmac(
'sha256',
password.encode('utf-8'),
salt.encode('utf-8'),
100000
)

return secrets.compare_digest(password_hash, stored_hash)
import pytest
import hashlib
import secrets
from auth import authenticate_user

class TestAuthenticateUserSecurity:
"""AI-generated security-focused test suite"""

def setup_method(self):
"""Set up test fixtures"""
self.test_password = "SecureP@ssw0rd123"
self.test_salt = "random_salt_123"
self.test_hash = hashlib.pbkdf2_hmac(
'sha256',
self.test_password.encode('utf-8'),
self.test_salt.encode('utf-8'),
100000
)

def test_valid_authentication(self):
"""Test successful authentication with valid credentials"""
result = authenticate_user("testuser", self.test_password, self.test_hash, self.test_salt)
assert result is True

def test_invalid_password_fails(self):
"""Test authentication fails with wrong password"""
result = authenticate_user("testuser", "wrong_password", self.test_hash, self.test_salt)
assert result is False

def test_empty_username_fails(self):
"""Test authentication fails with empty username"""
result = authenticate_user("", self.test_password, self.test_hash, self.test_salt)
assert result is False

def test_empty_password_fails(self):
"""Test authentication fails with empty password"""
result = authenticate_user("testuser", "", self.test_hash, self.test_salt)
assert result is False

def test_sql_injection_attempt(self):
"""Test resistance to SQL injection attempts"""
malicious_input = "'; DROP TABLE users; --"
result = authenticate_user(malicious_input, self.test_password, self.test_hash, self.test_salt)
assert result is False

def test_timing_attack_resistance(self):
"""Test that function execution time is consistent"""
import time

# Measure time for valid user
start = time.time()
authenticate_user("testuser", "wrong_password", self.test_hash, self.test_salt)
time_invalid = time.time() - start

# Measure time for non-existent user
start = time.time()
authenticate_user("nonexistent", "wrong_password", self.test_hash, self.test_salt)
time_nonexistent = time.time() - start

# Times should be similar (within 10ms)
assert abs(time_invalid - time_nonexistent) < 0.01

def test_unicode_password_handling(self):
"""Test proper handling of unicode characters in passwords"""
unicode_password = "пароль🔐"
unicode_hash = hashlib.pbkdf2_hmac(
'sha256',
unicode_password.encode('utf-8'),
self.test_salt.encode('utf-8'),
100000
)

result = authenticate_user("testuser", unicode_password, unicode_hash, self.test_salt)
assert result is True
✨ AI Analysis Results: The system identified this as a security-critical function and automatically generated tests for common attack vectors including SQL injection, timing attacks, and edge cases with unicode characters.
Comprehensive metrics to evaluate test quality and coverage
Line Coverage: Percentage of code lines executed
Function Coverage: Percentage of functions tested
{
"generation_summary": {
"total_files_analyzed": 15,
"total_functions_found": 87,
"total_tests_generated": 324,
"generation_time": "2.3 minutes",
"overall_quality_score": 92.5
},
"coverage_metrics": {
"line_coverage": 89.2,
"branch_coverage": 85.7,
"function_coverage": 100.0,
"security_coverage": 78.3
},
"test_distribution": {
"UNIT": 156,
"INTEGRATION": 89,
"EDGE_CASE": 45,
"SECURITY": 23,
"PERFORMANCE": 8,
"ERROR_HANDLING": 3
},
"priority_distribution": {
"CRITICAL": 34,
"HIGH": 78,
"MEDIUM": 145,
"LOW": 67
},
"file_results": [
{
"file_path": "src/calculator.py",
"functions_analyzed": 8,
"tests_generated": 32,
"coverage_percentage": 94.5,
"quality_score": 88.2,
"security_score": 85.0
}
]
}
Problem: "Invalid API key" or authentication errors
Verify your Gemini API key is correct
Check that the API key has proper permissions
Ensure the .env file is in the correct directory
Problem: Tests not generating or poor quality output
💡 Pro Tip: Enable verbose logging by setting the environment variable LOG_LEVEL=DEBUG to get detailed information about the generation process.
Like this project

Posted Jul 9, 2025

AI-driven multi-agent Python test generator: AST analysis, risk-based tests, security checks, LLM semantics, realistic mocks—boosts coverage & quality.

Likes

1

Views

0

Timeline

Jul 6, 2025 - Jul 7, 2025

Join 50k+ companies and 1M+ independents

Contra Logo

© 2025 Contra.Work Inc