你的點數不足以執行本工具(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,請改成你的模組名。遇到不確定我們會標註假設,絕不默默瞎猜。
喜歡這個結果?你的點數快用完了 —— 充値繼續使用 →