[go: up one dir, main page]

Add a hypothesis plugin

currently

from hypothesis import given, assume, example, strategies as st
from contractme import precondition, postcondition


@precondition(lambda x: x > 0)
@postcondition(lambda result: result > 0)
@postcondition(lambda x: result * result = x)
def square_root(x: int) -> float:
    # This will fail for x big enough (loss of precision of floats) 
    return x ** 0.5

@given(st.integers())
@example(656251480)
def test_square_root(x):
    assume(square_root.accepts(x))
    r = square_root(x)

with plugin

from contractme import precondition, postcondition
from contractme.hypothesis import search_failures

@precondition(lambda x: x > 0)
@postcondition(lambda result: result > 0)
@postcondition(lambda x: result * result = x)
def square_root(x: int) -> float:
    # This will fail for x big enough (loss of precision of floats) 
    return x ** 0.5

def test_square_root():
    search_failures(square_root, examples=[656251480])

reference

https://hypothesis.works/

Edited by Léo GERMOND