Essentials

Test Generation

Learn how to use AI Developer Assistant for automated test generation

Test Generation

AI Developer Assistant provides intelligent test generation capabilities that automatically detect programming languages and select appropriate testing frameworks, making it easy to create comprehensive test suites.

Basic Test Generation

Generate Tests for All Files

# Generate test suggestions for all files
ai-dev test-suggest

# Generate with verbose output
ai-dev test-suggest --verbose

Generate Tests for Specific Files

# Generate tests for specific files
ai-dev test-suggest --file-patterns "src/utils/helper.ts"

# Generate tests for multiple files
ai-dev test-suggest --file-patterns "src/**/*.ts"

# Generate tests with glob patterns
ai-dev test-suggest --file-patterns "src/components/**/*.{ts,tsx}"

Framework Auto-Detection

Language-Specific Framework Selection

AI Developer Assistant automatically detects programming languages and selects appropriate testing frameworks:

# Dart/Flutter - Flutter Test Framework
ai-dev test-suggest --file-patterns "lib/**/*.dart"

# TypeScript/JavaScript - Jest
ai-dev test-suggest --file-patterns "src/**/*.ts"

# Python - pytest
ai-dev test-suggest --file-patterns "**/*.py"

# Java - JUnit
ai-dev test-suggest --file-patterns "src/**/*.java"

# C# - NUnit
ai-dev test-suggest --file-patterns "src/**/*.cs"

# Go - Go Testing
ai-dev test-suggest --file-patterns "**/*.go"

# Rust - Cargo Test
ai-dev test-suggest --file-patterns "src/**/*.rs"

Supported Languages and Frameworks

LanguageFile ExtensionsTest FrameworkExample Command
Dart.dartFlutter Testai-dev test-suggest --file-patterns "lib/**/*.dart"
TypeScript.ts, .tsxJestai-dev test-suggest --file-patterns "src/**/*.ts"
JavaScript.js, .jsxJestai-dev test-suggest --file-patterns "src/**/*.js"
Python.pypytestai-dev test-suggest --file-patterns "**/*.py"
Java.javaJUnitai-dev test-suggest --file-patterns "src/**/*.java"
C#.csNUnitai-dev test-suggest --file-patterns "src/**/*.cs"
Go.goGo Testingai-dev test-suggest --file-patterns "**/*.go"
Rust.rsCargo Testai-dev test-suggest --file-patterns "src/**/*.rs"
PHP.phpPHPUnitai-dev test-suggest --file-patterns "**/*.php"
Ruby.rbRSpecai-dev test-suggest --file-patterns "**/*.rb"

Test Types

Unit Tests

# Generate unit tests
ai-dev test-suggest --test-type unit

# Generate unit tests for specific files
ai-dev test-suggest --file-patterns "src/utils/**/*.ts" --test-type unit

Integration Tests

# Generate integration tests
ai-dev test-suggest --test-type integration

# Generate integration tests for API endpoints
ai-dev test-suggest --file-patterns "src/api/**/*.ts" --test-type integration

End-to-End Tests

# Generate E2E tests
ai-dev test-suggest --test-type e2e

# Generate E2E tests for user flows
ai-dev test-suggest --file-patterns "src/pages/**/*.tsx" --test-type e2e

Performance Tests

# Generate performance tests
ai-dev test-suggest --test-type performance

# Generate performance tests for critical functions
ai-dev test-suggest --file-patterns "src/algorithms/**/*.ts" --test-type performance

Security Tests

# Generate security tests
ai-dev test-suggest --test-type security

# Generate security tests for authentication
ai-dev test-suggest --file-patterns "src/auth/**/*.ts" --test-type security

Advanced Test Generation

Include Setup and Teardown

# Include test setup code
ai-dev test-suggest --include-setup

# Include test teardown code
ai-dev test-suggest --include-teardown

# Include both setup and teardown
ai-dev test-suggest --include-setup --include-teardown

Coverage Targets

# Set coverage target
ai-dev test-suggest --coverage-target 80

# Generate tests with high coverage
ai-dev test-suggest --coverage-target 95

Custom Framework Selection

# Override automatic framework detection
ai-dev test-suggest --framework jest --file-patterns "**/*.ts"

# Use specific framework
ai-dev test-suggest --framework pytest --file-patterns "**/*.py"

Language-Specific Examples

TypeScript/JavaScript with Jest

# Generate Jest tests for TypeScript
ai-dev test-suggest --file-patterns "src/utils/helper.ts"

Example Generated Test:

import { helperFunction } from '../helper';

describe('helperFunction', () => {
  beforeEach(() => {
    // Setup code
  });

  afterEach(() => {
    // Teardown code
  });

  it('should return expected result for valid input', () => {
    const result = helperFunction('valid input');
    expect(result).toBe('expected output');
  });

  it('should handle edge cases', () => {
    const result = helperFunction('');
    expect(result).toBe('');
  });

  it('should throw error for invalid input', () => {
    expect(() => helperFunction(null)).toThrow();
  });
});

Python with pytest

# Generate pytest tests for Python
ai-dev test-suggest --file-patterns "src/utils/helper.py"

Example Generated Test:

import pytest
from src.utils.helper import helper_function

class TestHelperFunction:
    def setup_method(self):
        # Setup code
        pass

    def teardown_method(self):
        # Teardown code
        pass

    def test_valid_input(self):
        result = helper_function('valid input')
        assert result == 'expected output'

    def test_edge_cases(self):
        result = helper_function('')
        assert result == ''

    def test_invalid_input(self):
        with pytest.raises(ValueError):
            helper_function(None)

Dart/Flutter with Flutter Test

# Generate Flutter tests for Dart
ai-dev test-suggest --file-patterns "lib/utils/helper.dart"

Example Generated Test:

import 'package:flutter_test/flutter_test.dart';
import 'package:your_app/utils/helper.dart';

void main() {
  group('HelperFunction', () {
    setUp(() {
      // Setup code
    });

    tearDown(() {
      // Teardown code
    });

    test('should return expected result for valid input', () {
      final result = helperFunction('valid input');
      expect(result, equals('expected output'));
    });

    test('should handle edge cases', () {
      final result = helperFunction('');
      expect(result, equals(''));
    });

    test('should throw exception for invalid input', () {
      expect(() => helperFunction(null), throwsA(isA<ArgumentError>()));
    });
  });
}

Java with JUnit

# Generate JUnit tests for Java
ai-dev test-suggest --file-patterns "src/main/java/utils/Helper.java"

Example Generated Test:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class HelperTest {
    @BeforeEach
    void setUp() {
        // Setup code
    }

    @AfterEach
    void tearDown() {
        // Teardown code
    }

    @Test
    void testValidInput() {
        String result = Helper.helperFunction("valid input");
        assertEquals("expected output", result);
    }

    @Test
    void testEdgeCases() {
        String result = Helper.helperFunction("");
        assertEquals("", result);
    }

    @Test
    void testInvalidInput() {
        assertThrows(IllegalArgumentException.class, () -> {
            Helper.helperFunction(null);
        });
    }
}

Output Formats

Console Output (Default)

ai-dev test-suggest --file-patterns "src/utils/helper.ts"

Example Output:

🧪 Test Generation: src/utils/helper.ts

📋 Generated Tests:
   - Unit tests for helperFunction
   - Edge case testing
   - Error handling tests
   - Setup and teardown code

📁 Test File: tests/helper.test.ts
📊 Coverage Target: 80%
🎯 Framework: Jest

Markdown Output

ai-dev test-suggest --format markdown --output-path test-suggestions.md

Example Output:

# Test Generation Report

## Generated Tests

### src/utils/helper.ts
- **Framework**: Jest
- **Test Type**: Unit
- **Coverage Target**: 80%

#### Test Cases
1. **Valid Input Test**
   - Tests normal operation
   - Verifies expected output

2. **Edge Case Test**
   - Tests empty input
   - Tests boundary conditions

3. **Error Handling Test**
   - Tests invalid input
   - Verifies proper error handling

#### Setup and Teardown
- **Setup**: Initialize test environment
- **Teardown**: Clean up resources

JSON Output

ai-dev test-suggest --format json --output-path test-suggestions.json

Example Output:

{
  "summary": {
    "filesProcessed": 5,
    "testsGenerated": 25,
    "frameworks": ["jest", "pytest", "junit"],
    "coverageTarget": 80
  },
  "tests": [
    {
      "file": "src/utils/helper.ts",
      "framework": "jest",
      "testType": "unit",
      "testCases": [
        {
          "name": "should return expected result for valid input",
          "type": "positive",
          "description": "Tests normal operation"
        }
      ]
    }
  ]
}

Test Generation Workflows

Pre-commit Test Generation

# 1. Stage your changes
git add .

# 2. Generate tests for staged files
ai-dev test-suggest --staged

# 3. Review generated tests
# 4. Commit with tests
git commit -m "Add feature with tests"

Continuous Integration

# In CI pipeline
ai-dev test-suggest --format json --output-path test-suggestions.json

# Generate tests for new code
ai-dev test-suggest --file-patterns "src/**/*.ts" --test-type unit

Development Workflow

# 1. Write new code
# 2. Generate tests
ai-dev test-suggest --file-patterns "src/new-feature/**/*.ts"

# 3. Run generated tests
npm test

# 4. Iterate on tests and code

Best Practices

Test Coverage

  1. Set appropriate coverage targets:
ai-dev test-suggest --coverage-target 80
  1. Focus on critical code:
ai-dev test-suggest --file-patterns "src/auth/**/*.ts" --test-type security
  1. Include edge cases:
ai-dev test-suggest --include-setup --include-teardown

Test Organization

  1. Generate tests by feature:
ai-dev test-suggest --file-patterns "src/features/user/**/*.ts"
  1. Generate different test types:
ai-dev test-suggest --file-patterns "src/api/**/*.ts" --test-type integration
  1. Organize by test type:
ai-dev test-suggest --file-patterns "src/**/*.ts" --test-type unit

Framework Selection

  1. Use automatic detection:
ai-dev test-suggest --file-patterns "**/*.ts"  # Auto-detects Jest
  1. Override when needed:
ai-dev test-suggest --framework vitest --file-patterns "**/*.ts"
  1. Match team preferences:
ai-dev test-suggest --framework pytest --file-patterns "**/*.py"
Start with basic test generation and gradually incorporate more advanced features like custom frameworks and coverage targets as your testing practices mature.