360 Secure SkillHub
360 安全技能中心

test-gen

该 Skill 用于为函数或类生成单元测试代码,正文给出 Python(pytest) 与 JavaScript(Jest) 示例模板,并列出 AAA 模式、测试命名规范、边界值测试与 Mock 用法等原则,属于面向代码生成的提示型技能,未包含执行或集成步骤。

guyulong
热度 0.00
最近更新 2026-10-18 23:40:11
分类:开发依赖:内部调用大模型应用场景:代码生成

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add guyulong/cn-agent-skills/test-gen
Metadata
name
test-gen
description
单元测试生成器
category
代码质量
tags
["测试","单元测试","自动化"]
tools
["claude-code","cursor","copilot"]

单元测试生成器

使用场景

为函数或类自动生成单元测试代码。

测试框架

Python (pytest)

import pytest
from app.calculator import add, divide

class TestCalculator:
    def test_add_positive(self):
        assert add(1, 2) == 3
    
    def test_add_negative(self):
        assert add(-1, -2) == -3
    
    def test_add_zero(self):
        assert add(0, 0) == 0
    
    def test_divide_normal(self):
        assert divide(10, 2) == 5.0
    
    def test_divide_by_zero(self):
        with pytest.raises(ZeroDivisionError):
            divide(10, 0)
    
    @pytest.mark.parametrize("a,b,expected", [
        (1, 2, 3),
        (0, 0, 0),
        (-1, 1, 0),
        (100, 200, 300),
    ])
    def test_add_parametrize(self, a, b, expected):
        assert add(a, b) == expected

JavaScript (Jest)

const { add, divide } = require('./calculator');

describe('Calculator', () => {
    describe('add', () => {
        test('正数相加', () => {
            expect(add(1, 2)).toBe(3);
        });
        
        test('负数相加', () => {
            expect(add(-1, -2)).toBe(-3);
        });
        
        test('零值相加', () => {
            expect(add(0, 0)).toBe(0);
        });
    });
    
    describe('divide', () => {
        test('正常除法', () => {
            expect(divide(10, 2)).toBe(5.0);
        });
        
        test('除以零抛异常', () => {
            expect(() => divide(10, 0)).toThrow('Division by zero');
        });
    });
});

测试原则

AAA模式

def test_example():
    # Arrange - 准备测试数据
    user = User(name="张三", age=25)
    
    # Act - 执行被测试的操作
    result = user.is_adult()
    
    # Assert - 验证结果
    assert result is True

测试命名规范

# 格式: test_<被测函数>_<场景>_<预期结果>
def test_divide_by_zero_raises_error():
    pass

def test_login_with_wrong_password_returns_error():
    pass

def test_calculate_with_negative_number_returns_positive():
    pass

边界值测试

# 数值边界
def test_boundary():
    assert process(0) == expected  # 最小值
    assert process(MAX_VALUE) == expected  # 最大值
    assert process(-1) == expected  # 负数

# 字符串边界
def test_string_boundary():
    assert process("") == expected  # 空字符串
    assert process("a" * 10000) == expected  # 超长字符串

# 集合边界
def test_collection_boundary():
    assert process([]) == expected  # 空列表
    assert process([1]) == expected  # 单元素

Mock使用

from unittest.mock import Mock, patch

# Mock外部API
@patch('app.services.external_api')
def test_with_mock(mock_api):
    mock_api.get_user.return_value = {"name": "张三"}
    result = get_user_info(123)
    assert result["name"] == "张三"
    mock_api.get_user.assert_called_once_with(123)