热门角色不仅是灵感来源,更是你的效率助手。通过精挑细选的角色提示词,你可以快速生成高质量内容、提升创作灵感,并找到最契合你需求的解决方案。让创作更轻松,让价值更直接!
我们根据不同用户需求,持续更新角色库,让你总能找到合适的灵感入口。
本提示词专为Python开发场景设计,能够根据用户提供的Python函数自动生成高质量单元测试代码。通过系统化的分析流程,确保测试用例覆盖函数的核心功能、边界条件和异常情况。生成的测试代码符合Python单元测试最佳实践,包含清晰的断言说明和必要的测试固件设置,帮助开发者提升代码质量和测试效率。
以下为针对 summarize_text 函数的完整测试设计与实现,基于 pytest,覆盖正常流程、边界条件与异常场景,确保测试代码可读、可维护且可直接运行。
# test_summarize_text.py
import pytest
def summarize_text(text: str, max_sentences: int = 3, max_chars: int | None = None) -> str:
'''
概要: 从给定文本中提取若干关键句,保持原始顺序。
适用: 中英文与混合标点文本,保证输出可读性。
参数:
- text: 原文,支持包含换行、emoji与中文标点
- max_sentences: 最多包含的句子数,>=1
- max_chars: 可选,最终文本的最大字符数(不截断句中词)
行为:
1. 规范空白,将连续空白折叠为单一空格。
2. 以 . ! ? 。 ! ? 作为句末符进行切分,保留标点。
3. 去除重复句(大小写与空白差异视为同句)。
4. 按顺序选取不超过 max_sentences 的句子;若设置 max_chars,
在不破坏已加入句子的前提下尽量满足限制。
例子:
输入: '今天很冷!但是太阳很好。We still go hiking? 还是要出门。'
输出(2句): '今天很冷! We still go hiking?'
'''
if not isinstance(text, str):
raise TypeError('text must be str')
if not isinstance(max_sentences, int) or max_sentences < 1:
raise ValueError('max_sentences must be >= 1')
if max_chars is not None:
if not isinstance(max_chars, int) or max_chars < 10:
raise ValueError('max_chars must be None or >= 10')
import re
norm = re.sub(r'\s+', ' ', text.strip())
if not norm:
return ''
endings = set('。!?.!?')
sentences = []
buf = []
for ch in norm:
buf.append(ch)
if ch in endings:
s = ''.join(buf).strip()
if s:
sentences.append(s)
buf = []
if buf:
tail = ''.join(buf).strip()
if tail:
sentences.append(tail)
seen = set()
unique = []
for s in sentences:
key = re.sub(r'\s+', ' ', s).casefold()
if key not in seen:
seen.add(key)
unique.append(s)
selected = unique[:max_sentences]
if max_chars is not None:
out = []
curr = 0
for s in selected:
candidate = ((' ' if out else '') + s)
if curr + len(candidate) <= max_chars:
out.append(s)
curr += len(candidate)
else:
break
return ' '.join(out)
return ' '.join(selected)
# ----------------- Parameter validation tests -----------------
@pytest.mark.parametrize("bad_text", [None, 123, 3.14, b"bytes"])
def test_invalid_text_type_raises_typeerror(bad_text):
with pytest.raises(TypeError):
summarize_text(bad_text)
@pytest.mark.parametrize("bad_ms", [0, -1, 1.5, "2"])
def test_invalid_max_sentences_raises_valueerror(bad_ms):
with pytest.raises(ValueError):
summarize_text("ok.", max_sentences=bad_ms)
@pytest.mark.parametrize("bad_mc", [9, "100", 0, -5, 3.14])
def test_invalid_max_chars_raises_valueerror(bad_mc):
with pytest.raises(ValueError):
summarize_text("ok.", max_chars=bad_mc)
def test_max_chars_none_is_ok():
assert summarize_text("a.") == "a."
assert summarize_text("a.", max_chars=None) == "a."
# ----------------- Empty and whitespace-only input -----------------
@pytest.mark.parametrize("txt", ["", " ", "\n\t \n"])
def test_empty_or_whitespace_only_returns_empty(txt):
assert summarize_text(txt) == ""
# ----------------- Core behavior and ordering -----------------
def test_spec_example_two_sentences():
text = "今天很冷!但是太阳很好。We still go hiking? 还是要出门。"
out = summarize_text(text, max_sentences=2)
assert out == "今天很冷! We still go hiking?"
def test_default_max_sentences_three():
text = "今天很冷!但是太阳很好。We still go hiking? 还是要出门。"
out = summarize_text(text) # default 3
assert out == "今天很冷! 但是太阳很好。 We still go hiking?"
def test_tail_without_terminal_punctuation_is_included():
assert summarize_text("No punctuation at end") == "No punctuation at end"
# ----------------- Whitespace normalization -----------------
def test_whitespace_normalization_across_spaces_tabs_newlines():
text = "Hi \n\n there!\tHow are\tyou?\nGood."
out = summarize_text(text)
assert out == "Hi there! How are you? Good."
# ----------------- Deduplication (case/whitespace-insensitive) -----------------
def test_deduplication_case_and_whitespace_insensitive():
text = "Hello world! hello world! HELLO WORLD!"
out = summarize_text(text, max_sentences=5)
assert out == "Hello world!"
# ----------------- Edge: consecutive punctuations -----------------
def test_consecutive_punctuations_create_standalone_punct_sentence():
text = "Wow!! Wow!"
out = summarize_text(text, max_sentences=5)
# Splits into ["Wow!", "!", "Wow!"] then dedup -> ["Wow!", "!"]
assert out == "Wow! !"
# ----------------- max_sentences with duplicates -----------------
def test_max_sentences_respects_unique_sentences():
text = "A. A. B. C."
out = summarize_text(text, max_sentences=3)
assert out == "A. B. C."
# ----------------- max_chars behavior -----------------
def test_max_chars_exact_boundary_and_not_exceed():
text = "abcd. ef. ghi."
# lengths: "abcd."=5, " ef."=4, " ghi."=5
out = summarize_text(text, max_sentences=5, max_chars=9)
assert out == "abcd. ef."
out2 = summarize_text(text, max_sentences=5, max_chars=8)
assert out2 == "abcd."
def test_max_chars_first_sentence_too_long_returns_empty():
# "abcdefghij." length = 11 (> 10), with max_chars=10, nothing fits
text = "abcdefghij."
out = summarize_text(text, max_sentences=3, max_chars=10)
assert out == ""
def test_max_chars_partial_selection_two_vs_one():
text = "abcd. efghi. jk."
# "abcd."=5, " efghi."=7 -> 12 total fits, so two sentences if max_chars=12
out = summarize_text(text, max_sentences=3, max_chars=12)
assert out == "abcd. efghi."
# With max_chars=11, only the first fits
out2 = summarize_text(text, max_sentences=3, max_chars=11)
assert out2 == "abcd."
# ----------------- Unicode/Emoji and Chinese punctuation -----------------
def test_unicode_emoji_and_chinese_punctuation():
text = "今天天气☀️很好!我们去🏖️吗?好呀"
out = summarize_text(text, max_sentences=3)
assert out == "今天天气☀️很好! 我们去🏖️吗? 好呀"
# ----------------- Single punctuation as sentence -----------------
def test_single_punctuation_sentence():
assert summarize_text(".") == "."
以“更快、更稳、更省心”为核心价值,帮助研发团队在数分钟内生成可直接运行的 Python 单元测试:
新功能提交前,快速生成覆盖正常、边界与异常的单元测试;补齐遗留模块测试,提升评审通过率;避免上线后低级回归,稳住接口行为。
基于函数说明一键产出成体系用例与断言说明;识别覆盖盲区并补齐关键路径;沉淀可复用测试模板,提升回归效率。
用统一的测试规范把控质量;为流水线准备好可直接运行的测试,缩短迭代周期;在人员紧张时也能保持稳定交付。
将模板生成的提示词复制粘贴到您常用的 Chat 应用(如 ChatGPT、Claude 等),即可直接对话使用,无需额外开发。适合个人快速体验和轻量使用场景。
把提示词模板转化为 API,您的程序可任意修改模板参数,通过接口直接调用,轻松实现自动化与批量处理。适合开发者集成与业务系统嵌入。
在 MCP client 中配置对应的 server 地址,让您的 AI 应用自动调用提示词模板。适合高级用户和团队协作,让提示词在不同 AI 工具间无缝衔接。
半价获取高级提示词-优惠即将到期