py-test-creator

TotalClaw 作者 totalclaw

从 Python 函数签名和文档字符串自动生成与 pytest 兼容的单元测试模板

安装 / 下载方式

TotalClaw CLI推荐
totalclaw skills install shop:totalclaw:totalclaw~martinforsulu-neo-py-test-creator
cURL直接下载,无需登录
curl -fsSL https://skills.taituai.com/api/skills/totalclaw%3Atotalclaw~martinforsulu-neo-py-test-creator/file -o martinforsulu-neo-py-test-creator.md
# py-test-creator — Skill Documentation

## Overview

`py-test-creator` is an OpenClaw skill that automatically generates comprehensive pytest unit test templates from Python code. It parses function signatures, type hints, and docstrings to create actionable test scaffolding that covers edge cases and parameter combinations.

**Key features:**
- Parse Python functions with complex signatures (defaults, type hints, variadic args)
- Generate pytest-compatible test methods with proper assertions
- Handle both standalone functions and class methods
- Convert docstrings into test method documentation
- Create ready-to-use test files with correct imports and structure

## Installation

Dependencies are managed via `package.json`. Install with:

```bash
npm install
```

This will install the required Python packages (pytest, ast-parser utilities) and Node.js dependencies for the skill runner.

## Usage

Trigger the skill with natural language:

- "Create unit tests for this Python function"
- "Generate test templates from these function signatures"
- "Write pytest tests for my Python methods"
- "Create unit test scaffolding from docstrings"

The skill expects a Python file or code snippet as input and produces a corresponding test file.

## Input/Output

**Input:**  
A Python file path or raw code containing one or more functions/methods.

**Output:**  
A test file (e.g., `test_<original>.py`) containing pytest test functions with:
- `import pytest` statements
- Test fixtures for common parameter types
- Edge case coverage (None values, boundaries, invalid types)
- Parametrized tests where appropriate
- Docstrings explaining test purpose

**Example:**

Input (`utils.py`):
```python
def add(a: int, b: int) -> int:
    """Add two integers and return the result."""
    return a + b
```

Output (`test_utils.py`):
```python
import pytest
from utils import add

def test_add_basic_integers():
    """Test add function with basic positive integers."""
    assert add(1, 2) == 3

def test_add_negative_numbers():
    """Test add function with negative integers."""
    assert add(-1, -2) == -3

def test_add_zero():
    """Test add function with zero."""
    assert add(0, 5) == 5
    assert add(5, 0) == 5

def test_add_large_numbers():
    """Test add function with large integers."""
    assert add(1000000, 2000000) == 3000000
```

## Configuration

No configuration required. The skill uses default pytest conventions.

**Optional environment variables:**
- `PYTEST_STRICT`: Set to `true` to enable strict mark handling
- `TEST_COVERAGE`: Set to `true` to include coverage hints in generated tests

## Error Handling

The skill exits with non-zero status on:
- Invalid Python syntax
- Missing input file/code
- Permission errors writing output
- AST parsing failures

Error messages are logged to stderr.

## Limitations

Out of scope:
- Running or validating generated tests
- Integration/end-to-end testing
- Multi-language support (Python only)
- CI/CD integration
- Performance optimization of tests

## Files

The skill package includes:

```
skill/
├── SKILL.md           # This documentation
├── package.json       # NPM package definition
├── README.md          # Quick start guide
└── scripts/
    ├── main.py        # CLI entry point
    ├── parser.py      # Python AST parser
    ├── generator.py   # Test template generator
    └── cli.py         # Command-line interface
```

## Resources

- [pytest Documentation](https://docs.pytest.org/)
- [Python AST Module](https://docs.python.org/3/library/ast.html)
- [OpenClaw Skill System](https://docs.openclaw.ai)

## Support

For issues, feature requests, or contributions, visit:
- Repository: `openclaw/openclaw`
- Discord: https://discord.com/invite/clawd

## License

MIT © OpenClaw Contributors

---

## 中文说明

# py-test-creator — 技能文档

## 概述

`py-test-creator` 是一个 OpenClaw 技能,可从 Python 代码自动生成全面的 pytest 单元测试模板。它会解析函数签名、类型提示和文档字符串,创建可直接使用的测试脚手架,覆盖边界情况和参数组合。

**核心特性:**
- 解析具有复杂签名的 Python 函数(默认值、类型提示、可变参数)
- 生成带有正确断言、与 pytest 兼容的测试方法
- 同时支持独立函数和类方法
- 将文档字符串转换为测试方法的文档说明
- 创建带有正确导入和结构、可直接使用的测试文件

## 安装

依赖通过 `package.json` 管理。安装方式:

```bash
npm install
```

这将安装所需的 Python 包(pytest、ast-parser 相关工具)以及技能运行器所需的 Node.js 依赖。

## 用法

使用自然语言触发该技能:

- "Create unit tests for this Python function"
- "Generate test templates from these function signatures"
- "Write pytest tests for my Python methods"
- "Create unit test scaffolding from docstrings"

该技能以 Python 文件或代码片段作为输入,并生成对应的测试文件。

## 输入/输出

**输入:**  
包含一个或多个函数/方法的 Python 文件路径或原始代码。

**输出:**  
一个测试文件(例如 `test_<original>.py`),其中包含的 pytest 测试函数具备:
- `import pytest` 语句
- 针对常见参数类型的测试夹具(fixture)
- 边界情况覆盖(None 值、边界值、无效类型)
- 在合适处使用参数化测试
- 解释测试目的的文档字符串

**示例:**

输入(`utils.py`):
```python
def add(a: int, b: int) -> int:
    """Add two integers and return the result."""
    return a + b
```

输出(`test_utils.py`):
```python
import pytest
from utils import add

def test_add_basic_integers():
    """Test add function with basic positive integers."""
    assert add(1, 2) == 3

def test_add_negative_numbers():
    """Test add function with negative integers."""
    assert add(-1, -2) == -3

def test_add_zero():
    """Test add function with zero."""
    assert add(0, 5) == 5
    assert add(5, 0) == 5

def test_add_large_numbers():
    """Test add function with large integers."""
    assert add(1000000, 2000000) == 3000000
```

## 配置

无需配置。该技能使用默认的 pytest 约定。

**可选环境变量:**
- `PYTEST_STRICT`:设为 `true` 以启用严格的标记(mark)处理
- `TEST_COVERAGE`:设为 `true` 以在生成的测试中包含覆盖率提示

## 错误处理

该技能在以下情况下以非零状态退出:
- Python 语法无效
- 缺少输入文件/代码
- 写入输出时发生权限错误
- AST 解析失败

错误信息记录到 stderr。

## 限制

不在范围内:
- 运行或验证生成的测试
- 集成测试/端到端测试
- 多语言支持(仅限 Python)
- CI/CD 集成
- 测试的性能优化

## 文件

该技能包包含:

```
skill/
├── SKILL.md           # This documentation
├── package.json       # NPM package definition
├── README.md          # Quick start guide
└── scripts/
    ├── main.py        # CLI entry point
    ├── parser.py      # Python AST parser
    ├── generator.py   # Test template generator
    └── cli.py         # Command-line interface
```

## 资源

- [pytest Documentation](https://docs.pytest.org/)
- [Python AST Module](https://docs.python.org/3/library/ast.html)
- [OpenClaw Skill System](https://docs.openclaw.ai)

## 支持

如有问题、功能请求或贡献,请访问:
- Repository: `openclaw/openclaw`
- Discord: https://discord.com/invite/clawd

## 许可证

MIT © OpenClaw Contributors