Skip to content
test-generator logo

测试生成器

test-generator

自动生成单元测试:识别函数逻辑、覆盖边界条件、生成可运行测试代码

SKILL.md

Full skill instructions

测试生成器

触发条件

当用户要求"生成测试"、"写单元测试"、"为 XX 函数写测试"、"增加测试覆盖率"时激活此技能。

工作流程

1. 分析目标代码

  • 读取目标文件,识别函数/类的输入输出
  • 分析参数类型、返回值、副作用
  • 识别依赖关系(数据库、API、文件系统等)

2. 确定测试策略

  • 纯函数:参数化测试,覆盖正常/边界/异常输入
  • 类方法:测试初始化、正常调用、状态变化
  • 异步函数:测试成功路径、超时、网络错误
  • 含副作用的函数:使用 mock/​stub 隔离依赖

3. 生成测试用例

每个函数至少覆盖:

  • ✅ 正常输入(happy path)
  • ⚠️ 边界条件(空值、零值、极大值、空字符串/数组)
  • ❌ 异常输入(类型错误、格式错误)
  • 🔗 依赖失败(网络超时、数据库异常)

4. 输出格式

根据项目语言和测试框架生成对应代码:

Python (pytest)
import pytest
from mymodule import my_function

class TestMyFunction:
    def test_normal_input(self):
        assert my_function("hello") == "HELLO"
    
    def test_empty_string(self):
        assert my_function("") == ""
    
    def test_none_input(self):
        with pytest.raises(TypeError):
            my_function(None)
    
    def test_special_characters(self):
        assert my_function("你好!@#") == "你好!@#"
JavaScript (Jest)
const { myFunction } = require('./​mymodule');

describe('myFunction', () => {
  test('正常输入', () => {
    expect(myFunction('hello')).toBe('HELLO');
  });
  
  test('空字符串', () => {
    expect(myFunction('')).toBe('');
  });
  
  test('null 输入', () => {
    expect(() => myFunction(null)).toThrow(TypeError);
  });
});
Go (testing)
package mypackage

import "testing"

func TestMyFunction(t *testing.T) {
    tests := []struct {
        name  string
        input string
        want  string
    }{
        {"正常输入", "hello", "HELLO"},
        {"空字符串", "", ""},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := MyFunction(tt.input); got != tt.want {
                t.Errorf("MyFunction(%q) = %q, want %q", tt.input, got, tt.want)
            }
        })
    }
}

测试命名规范

  • 中文项目:test_函数名_场景描述 或 函数名_正常输入/边界条件/异常输入
  • 英文项目:遵循项目现有命名风格

最佳实践

  1. 先读再写:理解代码逻辑后再生成测试,不要盲目覆盖
  2. 独立性:每个测试独立运行,不依赖执行顺序
  3. 可读性:测试名即文档,一看就知道测什么
  4. 适度 mock:外部依赖必须 mock,内部逻辑尽量不 mock
  5. 渐进式:先覆盖核心路径,再补充边界条件

More Automation skills

Daily schedule and task summary combining calendar events and pending to-dos for a specified date.

17.5K 451.2K
View

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View
playwright-cli logo
Automation

playwright-cli

Browser automation with 40+ commands for navigation, interaction, form filling, and web testing.

13.8K 175.3K
View

Launch Chromium with real-device fingerprints for multi-account operations, web scraping, and AI agent automation.

883 111.7K
View
firecrawl logo
Automation

firecrawl

Web scraping, search, crawling, and browser automation with LLM-optimized markdown output.

644 102.7K
View
browser-use logo
Automation

browser-use

Fast, persistent browser automation with session continuity across sequential agent commands.

117.2K 96.7K
View
agents-sdk logo
Automation

agents-sdk

Build stateful AI agents on Cloudflare Workers with persistent state, RPC methods, scheduling, and workflow orchestration.

3K 88K
View

Agents & automation AI tools

K
Agents & automation

Komos AI

A tool to turn screen demos into automated workflows.

Freemium
View
Microsoft Power Automate logo
Agents & automation

Microsoft Power Automate

Automate Your Business Processes Effortlessly with Microsoft Power Automate

Free
View
Agentgpt logo
Agents & automation

Agentgpt

Deploy autonomous AI Agents effortlessly in your browser.

Free
View
Cheat Layer logo
Agents & automation

Cheat Layer

Automate Your Business with Cheat Layer's Cutting-Edge AI

Paid
View
Magical AI logo
Agents & automation

Magical AI

Get sh*t done faster with AI-powered writing and task automation.

Free
View
MagicFlow logo
Agents & automation

MagicFlow

Create AI workflows with drag-and-drop simplicity.

Free
View
PixieBrix logo
Agents & automation

PixieBrix

Extend your apps, accelerate work

Paid
View