property-based-testing by aj-geddes/useful-ai-prompts
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill property-based-testing基于属性的测试通过自动生成大量输入,验证代码是否满足通用的属性或不变式,而不是测试特定的示例。这种方法可以发现基于示例的测试经常遗漏的边界情况和错误。
最小工作示例:
# test_string_operations.py
import pytest
from hypothesis import given, strategies as st, assume, example
def reverse_string(s: str) -> str:
"""反转字符串。"""
return s[::-1]
class TestStringOperations:
@given(st.text())
def test_reverse_twice_returns_original(self, s):
"""属性:反转两次应返回原始字符串。"""
assert reverse_string(reverse_string(s)) == s
@given(st.text())
def test_reverse_length_unchanged(self, s):
"""属性:反转不改变长度。"""
assert len(reverse_string(s)) == len(s)
@given(st.text(min_size=1))
def test_reverse_first_becomes_last(self, s):
"""属性:反转后第一个字符变为最后一个。"""
reversed_s = reverse_string(s)
assert s[0] == reversed_s[-1]
assert s[-1] == reversed_s[0]
// ... (完整实现请参阅参考指南)
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
references/ 目录下的详细实现:
| 指南 | 内容 |
|---|---|
| Hypothesis for Python | Python 的 Hypothesis |
| fast-check for JavaScript/TypeScript | JavaScript/TypeScript 的 fast-check |
| junit-quickcheck for Java | Java 的 junit-quickcheck |
每周安装量
117
代码仓库
GitHub 星标数
141
首次出现
2026年1月21日
安全审计
安装于
opencode93
gemini-cli91
codex89
cursor88
claude-code86
github-copilot78
Property-based testing verifies that code satisfies general properties or invariants for a wide range of automatically generated inputs, rather than testing specific examples. This approach finds edge cases and bugs that example-based tests often miss.
Minimal working example:
# test_string_operations.py
import pytest
from hypothesis import given, strategies as st, assume, example
def reverse_string(s: str) -> str:
"""Reverse a string."""
return s[::-1]
class TestStringOperations:
@given(st.text())
def test_reverse_twice_returns_original(self, s):
"""Property: Reversing twice returns the original string."""
assert reverse_string(reverse_string(s)) == s
@given(st.text())
def test_reverse_length_unchanged(self, s):
"""Property: Reverse doesn't change length."""
assert len(reverse_string(s)) == len(s)
@given(st.text(min_size=1))
def test_reverse_first_becomes_last(self, s):
"""Property: First char becomes last after reverse."""
reversed_s = reverse_string(s)
assert s[0] == reversed_s[-1]
assert s[-1] == reversed_s[0]
// ... (see reference guides for full implementation)
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| Hypothesis for Python | Hypothesis for Python |
| fast-check for JavaScript/TypeScript | fast-check for JavaScript/TypeScript |
| junit-quickcheck for Java | junit-quickcheck for Java |
Weekly Installs
117
Repository
GitHub Stars
141
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode93
gemini-cli91
codex89
cursor88
claude-code86
github-copilot78
Skills CLI 使用指南:AI Agent 技能包管理器安装与管理教程
44,900 周安装