def test_sum_greater_than_max_small_numbers():
xs = [1, 2, 3]
assert sum( xs ) > max( xs )
def test_sum_greater_than_max_big_numbers():
xs = [1000, 2000, 3000]
assert sum( xs ) > max( xs )
( example extracted from Zac Hatfield-Dodds PyConAu 2018 slides )
import pytest
@pytest.mark.parameterize('xs',[
[1, 2, 3], [1000, 2000, 3000]
])
def test_sum_greater_than_max(xs):
assert sum( xs ) > max( xs )
( example extracted from Zac Hatfield-Dodds PyConAu 2018 slides )
from hypothesis import given
from hypothesis.strategies import lists, integers
@given(lists(integers()))
def test_sum_greater_than_max(xs):
assert sum( xs ) > max( xs )
( example extracted from Zac Hatfield-Dodds PyConAu 2018 slides )
from hypothesis import given
from hypothesis.strategies import lists, integers
@given(lists(integers()))
def test_sum_greater_than_max(xs):
assert sum( xs ) > max( xs )
Falsifying example: test_sum_greater_than_max(xs=[])
Traceback (most recent call last):
...
assert sum( xs ) > max( xs )
ValueError: max() arg is an empty sequence
( example extracted from Zac Hatfield-Dodds PyConAu 2018 slides )
from hypothesis import given
from hypothesis.strategies import lists, integers
@given(lists(integers(), min_size=1))
def test_sum_greater_than_max(xs):
assert sum( xs ) > max( xs )
Traceback (most recent call last):
...
assert sum( xs ) > max( xs )
AssertionError: assert 0 > 0
+ where 0 = sum([0])
+ and 0 = max([0])
----- Hypothesis --------
Falsifying example: test_sum_greater_than_max(xs=[0])
( example extracted from Zac Hatfield-Dodds PyConAu 2018 slides )
from hypothesis import given
from hypothesis.strategies import lists, integers
@given(lists(integers(), min_size=1))
def test_sum_greater_than_max(xs):
assert sum( xs ) >= max( xs )
Traceback (most recent call last):
...
assert sum( xs ) >= max( xs )
AssertionError: assert -1 >= 0
+ where -1 = sum([0, -1])
+ and 0 = max([0, -1])
----- Hypothesis ----------
Falsifying example: test_sum_greater_than_max(xs=[0, -1])
( example extracted from Zac Hatfield-Dodds PyConAu 2018 slides )
from hypothesis import given
from hypothesis.strategies import lists, integers
@given(lists(integers(min_value=0), min_size=1))
def test_sum_greater_than_max(xs):
assert sum( xs ) >= max( xs )
. [100%]
=========== 1 passed in 0.19 seconds ========
( example extracted from Zac Hatfield-Dodds PyConAu 2018 slides )
from hypothesis import strategies
# from hypothesis.strategies import ...
>> strategies.integers().example()
-20719
>> strategies.floats().example()
2.00001
>> strategies.decimals().example()
Decimal('NaN')
>> strategies.complex_numbers().example()
(5.835754834383092e+16-1.9j)
lists( integers() ), tuples( booleans() ),
dictionaries( text(), floats() ),
sets( characters() )
from hypothesis.extra.numpy import arrays
from hypothesis.extra.pandas import data_frames, columns
from hypothesis.extra.django import from_model
@given(lists(integers(), min_size=1))
def test_sum_greater_than_max(xs):
assert sum( xs ) >= max( xs )
...
Falsifying example: test_sum_greater_than_max(xs=[0, -1])
[-999,100,8] X
[-999,100] X
[-999,0] X
[0,0] ✓
[-1,0] X
Fred Hebert
assert text == json.loads(json.dumps(text))
assert awesome_new_function(x) == old_slow_function(x)
assert fancy_algorithm(x) == brute_force(x)
assert eat_cookies(x, threads=10) == eat_cookies(x, threads=1)
from hypothesis.stateful import RuleBasedStateMachine
class DatabaseComparison(RuleBasedStateMachine):
...
keys = Bundle('keys')
values = Bundle('values')
@rule(target=keys, k=st.binary())
def add_key(self, k):
...
@rule(target=values, v=st.binary())
def add_value(self, v):
...
@rule(k=keys, v=values)
def save(self, k, v):
...
@rule(k=keys, v=values)
def delete(self, k, v):
...
@rule(k=keys)
def values_agree(self, k):
...
AssertionError: assert set() == {b''}
------------ Hypothesis ------------
state = DatabaseComparison()
var1 = state.add_key(k=b'')
var2 = state.add_value(v=var1)
state.save(k=var1, v=var2)
state.delete(k=var1, v=var2)
state.values_agree(k=var1)
state.teardown()
Example base testing | Property based testing |
---|---|
Focus on low level detail | Focus on high level requirements |
Tedious to test | Properties define behaviour |
Lots of repetition | Randomly generated input |
Painful to mantain | Failure case minimisation |
John Huges, QuickCheck author.