HypothesisWorks/hypothesis
GitHub: HypothesisWorks/hypothesis
Hypothesis 是 Python 的属性测试库,通过随机生成与最小化失败示例,帮助发现难以预测的边界 Bug。
Stars: 8577 | Forks: 639
# 假设
* [官方网站](https://hypothesis.works/)
* [文档](https://hypothesis.readthedocs.io/en/latest/)
* [源代码](https://github.com/hypothesisWorks/hypothesis/)
* [贡献指南](https://github.com/HypothesisWorks/hypothesis/blob/master/CONTRIBUTING.rst)
* [社区](https://hypothesis.readthedocs.io/en/latest/community.html)
Hypothesis 是 Python 的属性测试库。使用 Hypothesis,你可以编写针对任意输入范围都应通过的测试,并让 Hypothesis 随机选择要检查的输入——包括你可能未考虑到的边界情况。例如:
```
from hypothesis import given, strategies as st
@given(st.lists(st.integers()))
def test_matches_builtin(ls):
assert sorted(ls) == my_sort(ls)
```
这种随机化测试可以发现你未曾想到、也不会主动寻找的 Bug。此外,当 Hypothesis 发现错误时,它不会只报告一个失败的示例,而是会报告**最简单的失败示例**。这使得属性测试成为调试和测试的强大工具。
例如,
```
def my_sort(ls):
return sorted(set(ls))
```
会失败并给出最简单的失败示例:
```
Falsifying example: test_matches_builtin(ls=[0, 0])
```
### 安装
要安装 Hypothesis:
```
pip install hypothesis
```
你还可以使用[可选扩展](https://hypothesis.readthedocs.io/en/latest/extras.html)。
标签:Hypothesis, property-based testing, pytest 插件, Python, Python 测试工具, 单元测试, 属性测试, 开源测试, 无后门, 测试库, 测试生成, 边缘案例, 逆向工具, 随机测试