Skip to main content

Auto-Pytester

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

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


✨ 特性

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

🚀 30 秒上手

1. 安装

pip install auto-pytester

🛠️ CLI 参考

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. 编写您的业务代码,当然只需要空壳子和示例数据即可

业务代码示例

# /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. 生成测试

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

生成的测试文件如下

"""
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. 执行测试

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! 🎉

Metadata

Release files for auto-pytester 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for auto-pytester 0.1.4
File Size Uploaded
auto_pytester-0.1.4.tar.gz 11.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for auto-pytester 0.1.4
File Interpreter ABI Platform
auto_pytester-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 25.4 kB

Release files / auto_pytester-0.1.4.tar.gz

Download URL auto_pytester-0.1.4.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ea8728f2e3740105f25b6951d2a54efd66efbfb358f5d62f11395a8d28001e1f
BLAKE2b-256 checksum
How to use checksums
217b9b458fdd3056626c1ccb28f8a73eb9c2bc6a82cad2944caa2927049a72f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release files / auto_pytester-0.1.4-py3-none-any.whl

Download URL auto_pytester-0.1.4-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
29ece610d73c599181030ba9ddf9dc17b61d64131d0195cd756f02b2e773bfd1
BLAKE2b-256 checksum
How to use checksums
b31d7314cf9e39f201a0cc4196c559c7508a119696988fdba284da7f4737c4ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page