# Auto-Pytester  
> *“先写函数签名 + 一行注释 → 自动生成完整单元测试”*

Auto-Pytester 帮你把 **TDD 的前置成本降到零**：  
只需在函数/方法里贴上带有示例的 docstring，即可一键生成 `pytest`/`unittest` 用例文件，保持代码与测试同步演进。

---

## ✨ 特性
- 🚀 **零配置** 复制粘贴即可用  
- 🧪 支持 **pytest** / **unittest** 双格式  
- 📦 支持 **YAML / JSON / TOML** 示例语法  
- 🐍 支持 **同步 & 异步** 函数  
- 🏗️ 支持 **嵌套类 / 继承 / 泛型**  
- 📁 支持 **monorepo** 多包扫描  

---

## 🚀 30 秒上手

### 1. 安装
```bash
pip install auto-pytester
```
🛠️ CLI 参考
```bash
tester@Paradox2024:~/app$ auto-pytest --help
usage: auto-pytest [-h] [-o OUTPUT_DIR] [-i INCLUDE] [-e EXCLUDE] [-I] [-v] project_dir

Auto generate pytest cases from docstring YAML examples

positional arguments:
  project_dir           Root of the Python project

options:
  -h, --help            show this help message and exit
  -o OUTPUT_DIR, --output-dir OUTPUT_DIR
                        Output dir for tests (default: tests-auto)
  -i INCLUDE, --include INCLUDE
                        Glob to include (repeatable)
  -e EXCLUDE, --exclude EXCLUDE
                        Glob to exclude (repeatable)
  -I, --inplace         Generate test_xxx.py next to sources
  -v, --verbose         Show verbose output
```


### 2. 编写您的业务代码，当然只需要空壳子和示例数据即可
#### 业务代码示例
```python
# /bin/bash

class Calc:
    @classmethod
    def add(cls, a: int, b: int) -> int:
        """
        加法

        Examples:
            >>> yaml
            doc: "expect 2 + 3 == 5"
            input:
              a: 2
              b: 3
            output: 5
            <<<
        """
        return 0
```

### 3. 生成测试
```bash
tester@Paradox2024:~/app$ auto-pytest -o testcases -v .
[INFO] Scanning ~/app
[scan_project] elapsed: 0.001ms
[render_tests] elapsed: 0.622ms
[INFO] Generated 7 test cases in ~/app/testcases
```
#### 生成的测试文件如下
```python
"""
Tests for class `Calc`
Generated by auto-pytester — DO NOT EDIT MANUALLY.
"""
import pytest

from src.calc import Calc


@pytest.fixture
def calc():
    return Calc()

def test_Calc_div_0(calc):
    result = calc.div(**{'a': 6, 'b': 3})
    assert result == 2

def test_Calc_div_1(calc):
    with pytest.raises(ValueError):
        result = calc.div(**{'a': 1, 'b': 0})
```

### 4. 执行测试
```bash
tester@Paradox2024:~/app$ pytest testcases/ -q 
======================= test session starts =======================
platform linux -- Python 3.11.13, pytest-8.4.1, pluggy-1.6.0 -- ~/app/.venv/bin/python3
cachedir: .pytest_cache
rootdir: ~/app
configfile: pytest.ini
plugins: allure-pytest-2.13.5, assume-2.4.3
collected 6 items

tests-auto/test_Calc.py::test_Calc_add_0 PASSED
tests-auto/test_Calc.py::test_Calc_add_1 PASSED
tests-auto/test_Calc.py::test_Calc_div_2 PASSED
tests-auto/test_Calc.py::test_Calc_div_3 PASSED
tests-auto/test_cx.py::test_cx_0 PASSED
tests-auto/test_cx.py::test_cx_1 PASSED

======================= 6 passed in 0.02s =======================
``` 

---

### 📜 License

#### MIT © EveryOne

让测试成为副产品，专注业务。Happy TDD! 🎉
