假设:Python 的基于属性的测试
Hypothesis: Property-Based Testing for Python

原始链接: https://hypothesis.readthedocs.io/en/latest/

假设是 Python 的基于属性的测试库。使用假设,你编写的测试应该对你描述的范围内的所有输入都通过,而假设会随机选择要检查的输入——包括你可能没有想到的边界情况。例如: from hypothesis import given, strategies as st @given(st.lists(st.integers() | st.floats())) def test_sort_correct(lst): # 假设生成随机数字列表进行测试 assert my_sort(lst) == sorted(lst) test_sort_correct() 你应该从教程开始,或者选择更简洁的快速入门。假设介绍。新用户应该从这里开始,或从更简洁的快速入门开始。将假设应用于特定场景的实用指南。面向加深你对假设理解的评论。技术 API 参考。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 假设:Python 的基于属性的测试 (hypothesis.readthedocs.io) 12 分,lwhsiao 1 小时前 | 隐藏 | 过去 | 收藏 | 讨论 考虑申请 YC 的 2026 年冬季批次!申请截止日期为 11 月 10 日 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Hypothesis is the property-based testing library for Python. With Hypothesis, you write tests which should pass for all inputs in whatever range you describe, and let Hypothesis randomly choose which of those inputs to check - including edge cases you might not have thought about. For example:

from hypothesis import given, strategies as st

@given(st.lists(st.integers() | st.floats()))
def test_sort_correct(lst):
    # hypothesis generates random lists of numbers to test
    assert my_sort(lst) == sorted(lst)

test_sort_correct()

You should start with the tutorial, or alternatively the more condensed quickstart.

An introduction to Hypothesis.

New users should start here, or with the more condensed quickstart.

Practical guides for applying Hypothesis in specific scenarios.

Commentary oriented towards deepening your understanding of Hypothesis.

Technical API reference.

联系我们 contact @ memedata.com