SuperAPI AI Directory

unit-test-generator

Testing & quality

View repository

Updated 10-16 06:19

About this skill

Unit Test Generator

Automatically generate unit tests from source code, supporting multiple languages and testing frameworks.

Automatically generate unit tests based on source code, supporting multiple languages and testing frameworks.

When to Use

Use this skill when the user requests the following operations:

  • Generate unit tests / Generate unit tests
  • Write tests for functions/classes / Write tests for functions/classes
  • Create test cases / Create test cases
  • Improve code coverage / Improve code coverage

Instructions

Analysis Steps / Analysis Steps

  1. Identify code - Determine the programming language and code structure
  2. Analyze functionality - Understand the inputs, outputs, and behavior of functions/methods
  3. Determine boundaries - Identify boundary conditions and edge cases
  4. Select framework - Choose an appropriate testing framework based on the language
  5. Generate tests - Write comprehensive test cases

Supported Languages and Frameworks / Supported Languages

Language Testing framework
Python pytest, unittest
JavaScript/TypeScript Jest, Mocha, Vitest
Java JUnit, TestNG
Go testing (built-in)
Rust cargo test (built-in)

Test Types / Test Types

  • Happy path testing - Verify expected behavior
  • Boundary condition testing - Test boundary values
  • Exception handling testing - Verify error handling
  • Null/empty input testing - Handle null value cases

Output Format / Output Format

Use templates/test-report.md template to format the output.

Use the templates/test-report.md template to format output.

## 单元测试生成报告 / Unit Test Generation Report

### 分析概述 / Analysis Summary
[代码分析总结]

### 生成的测试 / Generated Tests
[测试代码]

### 测试覆盖说明 / Coverage Notes
- 覆盖的场景
- 建议补充的测试

Examples

Input / Input

def divide(a: float, b: float) -> float:
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

Output / Output

Unit Test Generation Report

Analysis Overview

divide The function performs division and includes a division-by-zero check. It is necessary to test normal division, division-by-zero exceptions, boundary values, and other cases.

Generated Tests

import pytest
from your_module import divide

class TestDivide:
    """Tests for the divide function."""

    def test_divide_positive_numbers(self):
        """Test division of positive numbers."""
        assert divide(10, 2) == 5.0
        assert divide(7, 2) == 3.5

    def test_divide_negative_numbers(self):
        """Test division with negative numbers."""
        assert divide(-10, 2) == -5.0
        assert divide(10, -2) == -5.0
        assert divide(-10, -2) == 5.0

    def test_divide_by_zero_raises_error(self):
        """Test that dividing by zero raises ValueError."""
        with pytest.raises(ValueError, match="Cannot divide by zero"):
            divide(10, 0)

    def test_divide_zero_numerator(self):
        """Test division when numerator is zero."""
        assert divide(0, 5) == 0.0

    def test_divide_float_precision(self):
        """Test division with floating point numbers."""
        assert divide(1, 3) == pytest.approx(0.333333, rel=1e-5)

Test Coverage Description

  • ✅ Normal positive number division
  • ✅ Negative number division
  • ✅ Division-by-zero exception
  • ✅ Zero numerator
  • ✅ Floating-point precision