Finding more bugs with less work

https://bit.ly/finding-more-bugs-pycon-uk

David R. MacIver

drmaciver.com / @DRMacIver

?

from hypothesis import given
import hypothesis.strategies as st


def mean(xs):
    return sum(xs) / len(xs)


@given(st.lists(st.floats()))
def test_mean(xs):
    mean(xs)

ZeroDivisionError: division by zero
Falsifying example: test_mean(xs=[])

from hypothesis import given
import hypothesis.strategies as st


def mean(xs):
    return sum(xs) / (1 + len(xs))


@given(st.lists(st.floats()))
def test_mean(xs):
    mean(xs)

from hypothesis import given
import hypothesis.strategies as st


def mean(xs):
    return sum(xs) / (1 + len(xs))


@given(st.lists(st.floats()))
def test_mean(xs):
    m = mean(xs)
    if xs:
        assert min(xs) <= m <= max(xs)

assert 1.0 <= 0.5
  where 1.0 = min([1.0])
  and   0.5 = mean([1.0])

Falsifying example: test_mean(xs=[1.0])

from hypothesis import given
import hypothesis.strategies as st


def mean(xs):
    return sum(xs) / len(xs)


@given(st.lists(st.floats(), min_size=1))
def test_mean(xs):
    assert min(xs) <= mean(xs) <= max(xs)

assert nan <= nan
  where nan = min([nan])
  and   nan = mean([nan])

Falsifying example: test_mean(xs=[nan])


import math
from hypothesis import given, assume
import hypothesis.strategies as st


def mean(xs):
    return sum(xs) / len(xs)


@given(st.lists(st.floats(), min_size=1))
def test_mean(xs):
    assume(not any(math.isnan(x) for x in xs))
    assert min(xs) <= mean(xs) <= max(xs)

assert -inf <= nan
  where -inf = min([-inf, inf])
  and   nan = mean([-inf, inf])

Falsifying example: test_mean(xs=[-inf, inf])


import math
from hypothesis import given, assume
import hypothesis.strategies as st


def mean(xs):
    return sum(xs) / len(xs)


@given(st.lists(st.floats(), min_size=1))
def test_mean(xs):
    assume(not any(math.isnan(x) or math.isinf(x) for x in xs))
    assert min(xs) <= mean(xs) <= max(xs)

assert inf <= 8.98846567431158e+307
  where inf = mean(
    [8.988465674311579e+307, 8.98846567431158e+307])
  and   8.98846567431158e+307 = max(
    [8.988465674311579e+307, 8.98846567431158e+307])

Falsifying example: test_mean(
  xs=[8.988465674311579e+307, 8.98846567431158e+307])

import math
from hypothesis import given, assume
import hypothesis.strategies as st


def mean(xs):
    return sum(x / len(xs) for x in xs)


@given(st.lists(st.floats(), min_size=1))
def test_mean(xs):
    assume(not any(math.isnan(x) or math.isinf(x) for x in xs))
    assert min(xs) <= mean(xs) <= max(xs)

xs = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

assert 1.0 <= 0.9999999999999999
  where 1.0 = min([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
  and   0.9999999999999999 = mean([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])

Falsifying example: test_mean(xs=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0])

import math
from hypothesis import given, assume
import hypothesis.strategies as st


def mean(xs):
    return np.array(xs).mean()


@given(st.lists(st.floats(), min_size=1))
def test_mean(xs):
    assume(not any(math.isnan(x) or math.isinf(x) for x in xs))
    assert min(xs) <= mean(xs) <= max(xs)

assert inf <= 8.98846567431158e+307
  where inf = mean(
    [8.988465674311579e+307, 8.98846567431158e+307])
  and   8.98846567431158e+307 = max(
    [8.988465674311579e+307, 8.98846567431158e+307])

Falsifying example: test_mean(
  xs=[8.988465674311579e+307, 8.98846567431158e+307])

Further Reading

https://bit.ly/finding-more-bugs-pycon-uk