mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 14:50:19 +00:00
New testing tool: Hypothesis
Based on README at https://github.com/HypothesisWorks/hypothesis-python
This commit is contained in:
@@ -190,6 +190,40 @@ the unittest module!
|
||||
`py.test <https://docs.pytest.org/en/latest/>`_
|
||||
|
||||
|
||||
Hypothesis
|
||||
----------
|
||||
|
||||
Hypothesis is a library which lets you write tests that are parametrized by
|
||||
a source of examples. It then generates simple and comprehensible examples
|
||||
that make your tests fail, letting you find more bugs with less work.
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ pip install hypothesis
|
||||
|
||||
For example, testing lists of floats will try many examples, but report the
|
||||
minimal example of each bug (distinguished exception type and location):
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
|
||||
def test_mean(xs):
|
||||
mean = sum(xs) / len(xs)
|
||||
assert min(xs) <= mean(xs) <= max(xs)
|
||||
|
||||
.. code-block::
|
||||
|
||||
Falsifying example: test_mean(
|
||||
xs=[1.7976321109618856e+308, 6.102390043022755e+303]
|
||||
)
|
||||
|
||||
Hypothesis is practical as well as very powerful, and will often find bugs
|
||||
that escaped all other forms of testing. It integrates well with py.test,
|
||||
and has a strong focus on usability in both simple and advanced scenarios.
|
||||
|
||||
`hypothesis <https://hypothesis.readthedocs.io/en/latest/>`_
|
||||
|
||||
|
||||
tox
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user