跳到主内容
Panshi
EN / /
← 服务

🧪 单元测试生成

粘贴一个函数或模块,生成可运行的单测套件——覆盖正常路径、边界、非法输入和异常路径。

先看产出质量 —— 真实示例

示例 · 不消耗点数

输入 — 被测函数

框架:pytest

def apply_discount(price, percent):
    """按百分比打折后返回价格,保留 2 位小数。"""
    if price < 0:
        raise ValueError("price must be non-negative")
    if not 0 <= percent <= 100:
        raise ValueError("percent must be 0..100")
    return round(price * (1 - percent / 100), 2)

输出 — 生成的测试套件

import pytest
from pricing import apply_discount

def test_basic_discount():
    assert apply_discount(100.0, 20) == 80.0

def test_rounds_to_two_dp():
    assert apply_discount(9.99, 15) == 8.49

def test_zero_percent_is_unchanged():
    assert apply_discount(50.0, 0) == 50.0

def test_full_discount_is_zero():
    assert apply_discount(50.0, 100) == 0.0

def test_negative_price_raises():
    with pytest.raises(ValueError):
        apply_discount(-1.0, 10)

@pytest.mark.parametrize("p", [-5, 101])
def test_percent_out_of_range_raises(p):
    with pytest.raises(ValueError):
        apply_discount(100.0, p)

覆盖的用例

  • test_basic_discount — 正常路径,100 打 8 折
  • test_rounds_to_two_dp — 四舍五入到 2 位小数
  • test_zero/full_discount — 0% 与 100% 边界
  • test_negative_price_raises — 非法输入异常路径
  • test_percent_out_of_range_raises — 参数化 −5 和 101

说明: 假设 import 路径为 pricing,请改成你的模块名。遇到不确定我们会标注假设,绝不默默瞎猜。

相关工具

Token 计算器

精确数 token(o200k / GPT-4o),并对比 GPT/Claude/Gemini/DeepSeek/Qwen 各家输入输出成本 —— 浏览器本地运行。

API Key 泄漏自查

粘贴代码或配置,立刻找出硬编码密钥 —— OpenAI/AWS/GitHub/Stripe/Google key、私钥、JWT。100% 浏览器本地。

文本转 SQL

用大白话生成 PostgreSQL / MySQL / SQLite / BigQuery / Snowflake 的正确 SQL —— 理解表结构。

代码审查

贴一段 diff 或代码,得到分级审查 —— bug / 逻辑 / 安全 / 性能 —— 附修复建议。