Skip to content
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 / 邏輯 / 安全 / 效能 —— 附修復建議。