你的点数不足以运行本工具(9 点)。 购买点数 →
🧪 单元测试生成
粘贴一个函数或模块,生成可运行的单测套件——覆盖正常路径、边界、非法输入和异常路径。
- 能跑,不是伪代码。 真实的 import、setup 和断言,粘进仓库就能运行。
- 补上你会漏的用例。 边界、非法输入和异常路径,不只是正常流程。
- 你的框架。 pytest、Jest、JUnit——或按语言选最地道的默认框架。
先看产出质量 —— 真实示例
示例 · 不消耗点数输入 — 被测函数
框架: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,请改成你的模块名。遇到不确定我们会标注假设,绝不默默瞎猜。