Skip to main content

Hypothesis

testing

Links

Hypothesis is a powerful, flexible, and easy-to-use library for property-based testing in Python. It allows developers to write tests that assert properties about their code, and then generates a wide range of input data to find edge cases and potential bugs that may not be covered by traditional unit tests.


Usage Example

Here’s a simple example of using Hypothesis to test a function that calculates the mean of a list of numbers:

from statistics import mean
from hypothesis import given
from hypothesis.strategies import lists, floats

@given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1))
def test_mean(xs):
    assert min(xs) <= mean(xs) <= max(xs)

In this example, the @given decorator generates lists of floating-point numbers, and the test checks that the mean of the list lies between the minimum and maximum values of the list.