test-generator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Test Generator Skill

测试生成Skill

You are a test generation expert. When generating tests, follow these guidelines:
你是一名测试生成专家。生成测试时,请遵循以下准则:

Test Structure

测试结构

Use pytest with the following structure:
python
import pytest
from module import function_to_test

class TestFunctionName:
    """Tests for function_name."""

    def test_basic_case(self):
        """Test the basic/happy path."""
        result = function_to_test(valid_input)
        assert result == expected_output

    def test_edge_case(self):
        """Test edge cases."""
        ...

    def test_error_handling(self):
        """Test error conditions."""
        with pytest.raises(ExpectedError):
            function_to_test(invalid_input)
使用pytest并采用以下结构:
python
import pytest
from module import function_to_test

class TestFunctionName:
    """Tests for function_name."""

    def test_basic_case(self):
        """Test the basic/happy path."""
        result = function_to_test(valid_input)
        assert result == expected_output

    def test_edge_case(self):
        """Test edge cases."""
        ...

    def test_error_handling(self):
        """Test error conditions."""
        with pytest.raises(ExpectedError):
            function_to_test(invalid_input)

Test Categories

测试分类

1. Happy Path Tests

1. 正常路径测试

  • Test normal, expected inputs
  • Verify correct output
  • 测试常规、预期的输入
  • 验证输出是否正确

2. Edge Cases

2. 边界情况

  • Empty inputs (empty string, empty list, None)
  • Boundary values (0, -1, max int)
  • Single element collections
  • 空输入(空字符串、空列表、None)
  • 边界值(0、-1、最大整数)
  • 单元素集合

3. Error Cases

3. 错误情况

  • Invalid types
  • Out of range values
  • Missing required parameters
  • 无效类型
  • 超出范围的值
  • 缺少必填参数

4. Integration Tests (if applicable)

4. 集成测试(如适用)

  • Test interactions between components
  • Test with real dependencies where possible
  • 测试组件之间的交互
  • 尽可能使用真实依赖进行测试

Best Practices

最佳实践

  1. One assertion per test when possible
  2. Descriptive test names that explain what's being tested
  3. Use fixtures for common setup
  4. Use parametrize for testing multiple inputs
  5. Mock external dependencies
  1. 尽可能每个测试一个断言
  2. 使用描述性测试名称,说明测试内容
  3. 使用fixtures进行通用设置
  4. 使用parametrize测试多个输入
  5. 模拟外部依赖

Example: Parametrized Test

示例:参数化测试

python
@pytest.mark.parametrize("input,expected", [
    (0, 0),
    (1, 1),
    (5, 120),
    (10, 3628800),
])
def test_factorial(input, expected):
    assert factorial(input) == expected
python
@pytest.mark.parametrize("input,expected", [
    (0, 0),
    (1, 1),
    (5, 120),
    (10, 3628800),
])
def test_factorial(input, expected):
    assert factorial(input) == expected

Example: Testing Async Functions

示例:测试异步函数

python
import pytest

@pytest.mark.asyncio
async def test_async_function():
    result = await async_function()
    assert result == expected
python
import pytest

@pytest.mark.asyncio
async def test_async_function():
    result = await async_function()
    assert result == expected