Skip to main content

Auto generate pytest cases from docstring examples

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_pytester-0.1.4.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auto_pytester-0.1.4-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file auto_pytester-0.1.4.tar.gz.

File metadata

  • Download URL: auto_pytester-0.1.4.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for auto_pytester-0.1.4.tar.gz
Algorithm Hash digest
SHA256 ea8728f2e3740105f25b6951d2a54efd66efbfb358f5d62f11395a8d28001e1f
MD5 d20b4248d8ecaa822c92ff79b82f7352
BLAKE2b-256 217b9b458fdd3056626c1ccb28f8a73eb9c2bc6a82cad2944caa2927049a72f6

See more details on using hashes here.

File details

Details for the file auto_pytester-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: auto_pytester-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for auto_pytester-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 29ece610d73c599181030ba9ddf9dc17b61d64131d0195cd756f02b2e773bfd1
MD5 9c8a7ed1635f99b70a1a44c660bc7e23
BLAKE2b-256 b31d7314cf9e39f201a0cc4196c559c7508a119696988fdba284da7f4737c4ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page