def test_does_print_on_reuse_from_database():
    passes_healthcheck = False

    database = InMemoryExampleDatabase()

    @settings(database=database)
    @given(st.integers())
    def test(i):
        assume(passes_healthcheck)
        raise ValueError()

    with capture_out() as o:
        with pytest.raises(FailedHealthCheck):
            test()

    assert '@seed' in o.getvalue()

    passes_healthcheck = True

    with capture_out() as o:
        with pytest.raises(ValueError):
            test()

    assert all_values(database)
    assert '@seed' not in o.getvalue()

    passes_healthcheck = False

    with capture_out() as o:
        with pytest.raises(FailedHealthCheck):
            test()

    assert '@seed' in o.getvalue()
def test_stability():
    @given(
        st.lists(st.text(min_size=1, max_size=1), unique=True, min_size=5),
        st.choices(),
    )
    @settings(
        database=ExampleDatabase(),
    )
    def test_choose_and_then_fail(ls, choice):
        for _ in hrange(100):
            choice(ls)
        assert False

    # Run once first for easier debugging
    with raises(AssertionError):
        test_choose_and_then_fail()

    with capture_out() as o:
        with raises(AssertionError):
            test_choose_and_then_fail()
    out1 = o.getvalue()
    with capture_out() as o:
        with raises(AssertionError):
            test_choose_and_then_fail()
    out2 = o.getvalue()
    assert out1 == out2
    assert 'Choice #100:' in out1
def test_can_manually_call_initialize_rule():
    class StateMachine(RuleBasedStateMachine):
        initialize_called_counter = 0

        @initialize()
        def initialize(self):
            self.initialize_called_counter += 1
            return self.initialize_called_counter

        @rule()
        def fail_eventually(self):
            assert self.initialize() <= 2

    StateMachine.TestCase.settings = NO_BLOB_SETTINGS
    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(StateMachine)

    result = o.getvalue()
    assert (
        result
        == """\
Falsifying example: run_state_machine(factory=StateMachine, data=data(...))
state = StateMachine()
state.initialize()
state.fail_eventually()
state.fail_eventually()
state.teardown()
"""
    )
Esempio n. 4
0
def test_prints_debug_on_no_simplification():
    with Settings(verbosity=Verbosity.debug):
        with capture_out() as o:
            find(just(u"hi"), bool)
    v = o.getvalue()
    print(v)
    assert u"No simplifiers" in v
def test_prints_seed_on_exception(monkeypatch, in_pytest, fail_healthcheck):
    monkeypatch.setattr(core, 'running_under_pytest', in_pytest)

    strategy = st.integers()
    if fail_healthcheck:
        def slow_map(i):
            time.sleep(10)
            return i
        strategy = strategy.map(slow_map)
        expected_exc = FailedHealthCheck
    else:
        expected_exc = AssertionError

    @given(strategy)
    def test(i):
        assert False

    with capture_out() as o:
        with pytest.raises(expected_exc):
            test()

    output = o.getvalue()

    seed = test._hypothesis_internal_use_generated_seed
    assert seed is not None
    assert '@seed(%d)' % (seed,) in output
    contains_pytest_instruction = ('--hypothesis-seed=%d' % (seed,)) in output
    assert contains_pytest_instruction == in_pytest
def test_prints_equal_values_with_correct_variable_name():
    class MovesBetweenBundles(RuleBasedStateMachine):
        b1 = Bundle("b1")
        b2 = Bundle("b2")

        @rule(target=b1)
        def create(self):
            return []

        @rule(target=b2, source=b1)
        def transfer(self, source):
            return source

        @rule(source=b2)
        def fail(self, source):
            assert False

    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(MovesBetweenBundles)

    result = o.getvalue()
    for m in ["create", "transfer", "fail"]:
        assert result.count("state." + m) == 1
    assert "v1 = state.create()" in result
    assert "v2 = state.transfer(source=v1)" in result
    assert "state.fail(source=v2)" in result
Esempio n. 7
0
def test_prints_equal_values_with_correct_variable_name():
    class MovesBetweenBundles(RuleBasedStateMachine):
        b1 = Bundle('b1')
        b2 = Bundle('b2')

        @rule(target=b1)
        def create(self):
            return []

        @rule(target=b2, source=b1)
        def transfer(self, source):
            return source

        @rule(source=b2)
        def fail(self, source):
            assert False

    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(MovesBetweenBundles)

    result = o.getvalue()
    for m in ['create', 'transfer', 'fail']:
        assert result.count(m) == 1
    assert 'v1 = state.create()' in result
    assert 'v2 = state.transfer(source=v1)' in result
    assert 'state.fail(source=v2)' in result
Esempio n. 8
0
def test_initialize_rule_in_state_machine_with_inheritance():
    class ParentStateMachine(RuleBasedStateMachine):
        initialized = []

        @initialize()
        def initialize_a(self):
            self.initialized.append('a')

    class ChildStateMachine(ParentStateMachine):

        @initialize()
        def initialize_b(self):
            self.initialized.append('b')

        @rule()
        def fail_fast(self):
            assert False

    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(ChildStateMachine)

    assert set(ChildStateMachine.initialized[-2:]) == {'a', 'b'}
    result = o.getvalue().splitlines()
    assert result[0] == 'state = ChildStateMachine()'
    # Initialize rules call order is shuffled
    assert {result[1], result[2]} == {
        'state.initialize_a()', 'state.initialize_b()'}
    assert result[3] == 'state.fail_fast()'
    assert result[4] == 'state.teardown()'
Esempio n. 9
0
def test_initialize_rule():
    class WithInitializeRules(RuleBasedStateMachine):
        initialized = []

        @initialize()
        def initialize_a(self):
            self.initialized.append('a')

        @initialize()
        def initialize_b(self):
            self.initialized.append('b')

        @initialize()
        def initialize_c(self):
            self.initialized.append('c')

        @rule()
        def fail_fast(self):
            assert False

    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(WithInitializeRules)

    assert set(WithInitializeRules.initialized[-3:]) == {'a', 'b', 'c'}
    result = o.getvalue().splitlines()
    assert result[0] == 'state = WithInitializeRules()'
    # Initialize rules call order is shuffled
    assert {result[1], result[2], result[3]} == {
        'state.initialize_a()', 'state.initialize_b()', 'state.initialize_c()'
    }
    assert result[4] == 'state.fail_fast()'
    assert result[5] == 'state.teardown()'
Esempio n. 10
0
def test_shrinks_both_failures():
    first_has_failed = [False]
    duds = set()
    second_target = [None]

    @given(st.integers())
    def test(i):
        if i >= 10000:
            first_has_failed[0] = True
            assert False
        assert i < 10000
        if first_has_failed[0]:
            if second_target[0] is None:
                for j in range(10000):
                    if j not in duds:
                        second_target[0] = j
                        break
            assert i < second_target[0]
        else:
            duds.add(i)

    with capture_out() as o:
        with pytest.raises(MultipleFailures):
            test()

    assert 'test(i=10000)' in o.getvalue()
    assert 'test(i=%d)' % (second_target[0],) in o.getvalue()
def test_initialize_rule_populate_bundle():
    class WithInitializeBundleRules(RuleBasedStateMachine):
        a = Bundle("a")

        @initialize(target=a, dep=just("dep"))
        def initialize_a(self, dep):
            return "a v1 with (%s)" % dep

        @rule(param=a)
        def fail_fast(self, param):
            assert False

    WithInitializeBundleRules.TestCase.settings = NO_BLOB_SETTINGS
    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(WithInitializeBundleRules)

    result = o.getvalue()
    assert (
        result
        == """\
Falsifying example: run_state_machine(\
factory=WithInitializeBundleRules, data=data(...))
state = WithInitializeBundleRules()
v1 = state.initialize_a(dep='dep')
state.fail_fast(param=v1)
state.teardown()
"""
    )
Esempio n. 12
0
def test_multiple_rules_same_func():
    test_class = MultipleRulesSameFuncMachine.TestCase
    with capture_out() as o:
        test_class().runTest()
    output = o.getvalue()
    assert 'rule1data' in output
    assert 'rule2data' in output
Esempio n. 13
0
def test_does_not_print_notes_if_all_succeed():
    @given(integers())
    def test(i):
        note('Hi there')
    with capture_out() as out:
        with reporting.with_reporter(reporting.default):
            test()
        assert not out.getvalue()
def test_does_print_debug_in_debug():
    @given(integers())
    @settings(verbosity=Verbosity.debug)
    def f(x):
        debug_report("Hi")

    with capture_out() as o:
        f()
    assert u"Hi" in o.getvalue()
def test_does_not_print_reproduction_for_simple_examples_by_default():
    @given(st.integers())
    def test(i):
        assert False

    with capture_out() as o:
        with pytest.raises(AssertionError):
            test()
    assert '@reproduce_failure' not in o.getvalue()
Esempio n. 16
0
def test_does_not_print_debug_in_verbose():
    @given(integers())
    @settings(verbosity=Verbosity.verbose)
    def f(x):
        debug_report('Hi')

    with capture_out() as o:
        f()
    assert u'Hi' not in o.getvalue()
def test_can_seed_random():
    with capture_out() as out:
        with reporting.with_reporter(reporting.default):
            with pytest.raises(AssertionError):
                @given(st.random_module())
                def test(r):
                    assert False
                test()
    assert 'random.seed(0)' in out.getvalue()
Esempio n. 18
0
def test_does_print_verbose_in_debug():
    @given(integers())
    @settings(verbosity=Verbosity.debug)
    def f(x):
        verbose_report('Hi')

    with capture_out() as o:
        f()
    assert u'Hi' in o.getvalue()
def test_can_suppress_output():
    @given(integers())
    def test_int(x):
        assert False

    with capture_out() as o:
        with reporting.with_reporter(reporting.silent):
            with pytest.raises(AssertionError):
                test_int()
    assert u'Falsifying example' not in o.getvalue()
Esempio n. 20
0
def test_suppresses_exceptions_in_teardown():
    with capture_out() as o:
        with pytest.raises(AssertionError):
            with BuildContext():
                def foo():
                    raise ValueError()
                cleanup(foo)
                assert False

    assert u'ValueError' in o.getvalue()
def test_does_print_reproduction_given_an_invalid_repr():
    @given(st.integers().map(lambda x: Foo()))
    def test(i):
        raise ValueError()

    with capture_out() as o:
        with pytest.raises(ValueError):
            test()

    assert '@reproduce_failure' in o.getvalue()
def test_does_print_reproduction_for_simple_data_examples_by_default():
    @given(st.data())
    def test(data):
        data.draw(st.integers())
        assert False

    with capture_out() as o:
        with pytest.raises(AssertionError):
            test()
    assert '@reproduce_failure' in o.getvalue()
def test_prints_output_by_default():
    @given(integers())
    def test_int(x):
        assert False

    with capture_out() as o:
        with reporting.with_reporter(reporting.default):
            with pytest.raises(AssertionError):
                test_int()
    assert u'Falsifying example' in o.getvalue()
def test_does_not_print_on_success():
    @given(int)
    def test_is_an_int(x):
        return True

    with capture_out() as out:
        test_is_an_int()
    out = out.getvalue()
    lines = [l.strip() for l in out.split('\n')]
    assert all(not l for l in lines)
Esempio n. 25
0
def test_deadlines_participate_in_shrinking():
    @settings(deadline=500)
    @given(st.integers())
    def slow_if_large(i):
        if i >= 10000:
            time.sleep(1)

    with capture_out() as o:
        with pytest.raises(DeadlineExceeded):
            slow_if_large()
    assert 'slow_if_large(i=10000)' in o.getvalue()
def test_does_not_print_reproduction_if_told_not_to():
    @settings(print_blob=PrintSettings.NEVER)
    @given(st.integers().map(lambda x: Foo()))
    def test(i):
        raise ValueError()

    with capture_out() as o:
        with pytest.raises(ValueError):
            test()

    assert '@reproduce_failure' not in o.getvalue()
Esempio n. 27
0
def test_does_not_print_on_success():
    with Settings(verbosity=Verbosity.normal):
        @given(integers())
        def test_is_an_int(x):
            return True

        with capture_out() as out:
            test_is_an_int()
    out = out.getvalue()
    lines = [l.strip() for l in out.split(u'\n')]
    assert all(not l for l in lines), lines
def test_does_not_print_reproduction_if_verbosity_set_to_quiet():
    @given(st.data())
    @settings(verbosity=Verbosity.quiet)
    def test_always_fails(data):
        assert data.draw(st.just(False))

    with capture_out() as out:
        with pytest.raises(AssertionError):
            test_always_fails()

    assert '@reproduce_failure' not in out.getvalue()
Esempio n. 29
0
def test_prints_output_for_explicit_examples():
    @example(-1)
    @given(integers())
    def test_positive(x):
        assert x > 0

    with reporting.with_reporter(reporting.default):
        with pytest.raises(AssertionError):
            with capture_out() as out:
                test_positive()
    out = out.getvalue()
    assert u'Falsifying example: test_positive(x=-1)' in out
Esempio n. 30
0
def test_does_not_print_on_explicit_examples_if_no_failure():
    @example(1)
    @given(integers())
    def test_positive(x):
        assert x > 0

    with reporting.with_reporter(reporting.default):
        with pytest.raises(AssertionError):
            with capture_out() as out:
                test_positive()
    out = out.getvalue()
    assert u'Falsifying example: test_positive(1)' not in out
Esempio n. 31
0
def test_prints_on_failure_by_default():
    @given(integers(), integers())
    @settings(max_examples=100)
    def test_ints_are_sorted(balthazar, evans):
        assume(evans >= 0)
        assert balthazar <= evans

    with raises(AssertionError):
        with capture_out() as out:
            with reporting.with_reporter(reporting.default):
                test_ints_are_sorted()
    out = out.getvalue()
    lines = [l.strip() for l in out.split("\n")]
    assert "Falsifying example: test_ints_are_sorted(balthazar=1, evans=0)" in lines
Esempio n. 32
0
def test_invariant_present_in_falsifying_example():
    @Settings(print_blob=False)
    class BadRuleWithGoodInvariants(RuleBasedStateMachine):
        def __init__(self):
            super().__init__()
            self.num = 0

        @initialize()
        def initialize_1(self):
            pass

        @invariant(check_during_init=True)
        def invariant_1(self):
            pass

        @invariant(check_during_init=False)
        def invariant_2(self):
            pass

        @precondition(lambda self: self.num > 0)
        @invariant()
        def invariant_3(self):
            pass

        @rule()
        def rule_1(self):
            self.num += 1
            if self.num == 2:
                raise ValueError()

    with capture_out() as o:
        with pytest.raises(ValueError):
            run_state_machine_as_test(BadRuleWithGoodInvariants)

    result = o.getvalue()
    assert (result == """\
Falsifying example:
state = BadRuleWithGoodInvariants()
state.invariant_1()
state.initialize_1()
state.invariant_1()
state.invariant_2()
state.rule_1()
state.invariant_1()
state.invariant_2()
state.invariant_3()
state.rule_1()
state.teardown()
""")
Esempio n. 33
0
def test_removes_needless_steps():
    """Regression test from an example based on
    tests/nocover/test_database_agreement.py, but without the expensive bits.
    Comparing two database implementations in which deletion is broken, so as
    soon as a key/value pair is successfully deleted the test will now fail if
    you ever check that key.

    The main interesting feature of this is that it has a lot of
    opportunities to generate keys and values before it actually fails,
    but will still fail with very high probability.
    """

    @Settings(derandomize=True, max_examples=1000, deadline=None)
    class IncorrectDeletion(RuleBasedStateMachine):
        def __init__(self):
            super().__init__()
            self.__saved = defaultdict(set)
            self.__deleted = defaultdict(set)

        keys = Bundle("keys")
        values = Bundle("values")

        @rule(target=keys, k=binary())
        def k(self, k):
            return k

        @rule(target=values, v=binary())
        def v(self, v):
            return v

        @rule(k=keys, v=values)
        def save(self, k, v):
            self.__saved[k].add(v)

        @rule(k=keys, v=values)
        def delete(self, k, v):
            if v in self.__saved[k]:
                self.__deleted[k].add(v)

        @rule(k=keys)
        def values_agree(self, k):
            assert not self.__deleted[k]

    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(IncorrectDeletion)

    assert o.getvalue().count(" = state.k(") == 1
    assert o.getvalue().count(" = state.v(") == 1
Esempio n. 34
0
def test_error_in_strategy_produces_only_one_traceback():
    def boom(x):
        raise ValueError()

    with Settings(strict=False):

        @given(st.integers().map(boom))
        def test(x):
            pass

        with raises(ValueError):
            with reporting.with_reporter(reporting.default):
                with capture_out() as out:
                    test()
    assert out.getvalue().count('ValueError') == 2
Esempio n. 35
0
def test_prints_notes_once_on_failure():
    @given(lists(integers()))
    @settings(database=None, verbosity=Verbosity.normal)
    def test(xs):
        note('Hi there')
        if sum(xs) <= 100:
            raise ValueError()

    with capture_out() as out:
        with reporting.with_reporter(reporting.default):
            with raises(ValueError):
                test()
    lines = out.getvalue().strip().splitlines()
    assert len(lines) == 2
    assert 'Hi there' in lines
Esempio n. 36
0
def test_when_set_to_no_simplifies_only_runs_failing_example_once():
    failing = [0]

    @given(integers(), settings=hs.Settings(max_shrinks=0))
    def foo(x):
        if x > 11:
            failing[0] += 1
            assert False

    with hs.Settings(verbosity=hs.Verbosity.normal):
        with pytest.raises(AssertionError):
            with capture_out() as out:
                foo()
    assert failing == [1]
    assert 'Trying example' in out.getvalue()
Esempio n. 37
0
def test_gives_a_deadline_specific_flaky_error_message():
    once = [True]

    @settings(deadline=100)
    @given(st.integers())
    def slow_once(i):
        if once[0]:
            once[0] = False
            time.sleep(0.2)

    with capture_out() as o:
        with pytest.raises(Flaky):
            slow_once()
    assert "Unreliable test timing" in o.getvalue()
    assert "took 2" in o.getvalue()
Esempio n. 38
0
def test_prints_note_in_failing_example():
    @example(x=42)
    @example(x=43)
    @given(integers())
    def test(x):
        note(f"x -> {x}")
        assert x == 42

    with capture_out() as out:
        with reporting.with_reporter(reporting.default):
            with pytest.raises(AssertionError):
                test()
    v = out.getvalue()
    print(v)
    assert "x -> 43" in v
    assert "x -> 42" not in v
Esempio n. 39
0
def test_runs_multiple_cleanup_with_teardown():
    with capture_out() as o:
        with pytest.raises(AssertionError):
            with BuildContext():
                def foo():
                    raise ValueError()
                cleanup(foo)

                def bar():
                    raise TypeError()
                cleanup(foo)
                cleanup(bar)
                assert False

    assert u'ValueError' in o.getvalue()
    assert u'TypeError' in o.getvalue()
def test_prints_note_in_failing_example():
    @example(x=42)
    @example(x=43)
    @given(integers())
    def test(x):
        note('x -> %d' % (x, ))
        assert x == 42

    with capture_out() as out:
        with reporting.with_reporter(reporting.default):
            with pytest.raises(AssertionError):
                test()
    v = out.getvalue()
    print_unicode(v)
    assert 'x -> 43' in v
    assert 'x -> 42' not in v
Esempio n. 41
0
def test_prints_on_failure():
    @given(st.data())
    def test(data):
        x = data.draw(st.lists(st.integers(0, 10), min_size=2))
        y = data.draw(st.sampled_from(x))
        x.remove(y)
        if y in x:
            raise ValueError()

    with raises(ValueError):
        with capture_out() as out:
            with reporting.with_reporter(reporting.default):
                test()
    result = out.getvalue()
    assert 'Draw 1: [0, 0]' in result
    assert 'Draw 2: 0' in result
def test_prints_labels_if_given_on_failure():
    @given(st.data())
    def test(data):
        x = data.draw(st.lists(st.integers(0, 10), min_size=2), label="Some numbers")
        y = data.draw(st.sampled_from(x), label="A number")
        assert y in x
        x.remove(y)
        assert y not in x

    with raises(AssertionError):
        with capture_out() as out:
            with reporting.with_reporter(reporting.default):
                test()
    result = out.getvalue()
    assert "Draw 1 (Some numbers): [0, 0]" in result
    assert "Draw 2 (A number): 0" in result
Esempio n. 43
0
def test_inserts_line_breaks_only_at_appropriate_lengths(line_break, input):
    @example(input, input)
    @given(st.text(), st.text())
    def test(x, y):
        assert x < y

    with capture_out() as cap:
        with pytest.raises(AssertionError):
            test()

    template = OUTPUT_WITH_LINE_BREAK if line_break else OUTPUT_NO_LINE_BREAK

    desired_output = template % {"input": repr(input)}

    actual_output = cap.getvalue()

    assert desired_output.strip() == actual_output.strip()
def test_when_set_to_no_simplifies_runs_failing_example_twice():
    failing = [0]

    @given(integers())
    @settings(phases=no_shrink, max_examples=100, verbosity=Verbosity.normal)
    def foo(x):
        if x > 11:
            note('Lo')
            failing[0] += 1
            assert False

    with raises(AssertionError):
        with capture_out() as out:
            foo()
    assert failing == [2]
    assert 'Falsifying example' in out.getvalue()
    assert 'Lo' in out.getvalue()
Esempio n. 45
0
def test_uses_global_force(monkeypatch):
    monkeypatch.setattr(core, "global_force_seed", 42)

    @given(st.integers())
    def test(i):
        raise ValueError()

    output = []

    for _ in range(2):
        with capture_out() as o:
            with pytest.raises(ValueError):
                test()
        output.append(o.getvalue())

    assert output[0] == output[1]
    assert "@seed" not in output[0]
Esempio n. 46
0
def test_no_falsifying_example_if_unittest_skip(skip_exception):
    """If a ``SkipTest`` exception is raised during a test, Hypothesis should
    not continue running the test and shrink process, nor should it print
    anything about falsifying examples."""
    class DemoTest(unittest.TestCase):
        @given(xs=integers())
        def test_to_be_skipped(self, xs):
            if xs == 0:
                raise skip_exception
            else:
                assert xs == 0

    with capture_out() as o:
        suite = unittest.defaultTestLoader.loadTestsFromTestCase(DemoTest)
        unittest.TextTestRunner().run(suite)

    assert "Falsifying example" not in o.getvalue()
def test_raises_multiple_failures_with_varying_type():
    target = [None]

    @given(st.integers())
    def test(i):
        if abs(i) < 1000:
            return
        if target[0] is None:
            target[0] = i
        exc_class = TypeError if target[0] == i else ValueError
        raise exc_class()

    with capture_out() as o:
        with pytest.raises(MultipleFailures):
            test()

    assert 'TypeError' in o.getvalue()
    assert 'ValueError' in o.getvalue()
Esempio n. 48
0
def test_initialize_rule_populate_bundle():
    class WithInitializeBundleRules(RuleBasedStateMachine):
        a = Bundle('a')

        @initialize(target=a, dep=just('dep'))
        def initialize_a(self, dep):
            return 'a v1 with (%s)' % dep

        @rule(param=a)
        def fail_fast(self, param):
            assert False

    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(WithInitializeBundleRules)

    result = o.getvalue()
    assert result == """state = WithInitializeBundleRules()
def test_raises_multiple_failures_when_position_varies():
    target = [None]

    @given(st.integers())
    def test(i):
        if abs(i) < 1000:
            return
        if target[0] is None:
            target[0] = i
        if target[0] == i:
            raise ValueError('loc 1')
        else:
            raise ValueError('loc 2')

    with capture_out() as o:
        with pytest.raises(MultipleFailures):
            test()
    assert 'loc 1' in o.getvalue()
    assert 'loc 2' in o.getvalue()
Esempio n. 50
0
def test_prints_all_notes_in_verbose_mode():
    # slightly roundabout because @example messes with verbosity - see #1521
    messages = set()

    @settings(verbosity=Verbosity.debug, database=None)
    @given(integers(1, 10))
    def test(x):
        msg = 'x -> %d' % (x, )
        note(msg)
        messages.add(msg)
        assert x < 5

    with capture_out() as out:
        with reporting.with_reporter(reporting.default):
            with pytest.raises(AssertionError):
                test()
    v = out.getvalue()
    for x in sorted(messages):
        assert x in v
Esempio n. 51
0
def test_can_manually_call_initialize_rule():
    class StateMachine(RuleBasedStateMachine):
        initialize_called_counter = 0

        @initialize()
        def initialize(self):
            self.initialize_called_counter += 1
            return self.initialize_called_counter

        @rule()
        def fail_eventually(self):
            assert self.initialize() == 2

    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(StateMachine)

    result = o.getvalue()
    assert result == """state = StateMachine()
Esempio n. 52
0
def test_arguments_do_not_use_names_of_return_values():
    # See https://github.com/HypothesisWorks/hypothesis/issues/2341
    class TrickyPrintingMachine(RuleBasedStateMachine):
        data = Bundle("data")

        @initialize(target=data, value=integers())
        def init_data(self, value):
            return value

        @rule(d=data)
        def mostly_fails(self, d):
            assert d == 42

    with capture_out() as o:
        with pytest.raises(AssertionError):
            run_state_machine_as_test(TrickyPrintingMachine)
    output = o.getvalue()
    assert "v1 = state.init_data(value=0)" in output
    assert "v1 = state.init_data(value=v1)" not in output
Esempio n. 53
0
def test_raises_multiple_failures_when_position_varies():
    target = [None]

    @settings(max_examples=100)
    @given(st.integers())
    def test(i):
        if abs(i) < 1000:
            return
        if target[0] is None:
            target[0] = i
        if target[0] == i:
            raise ValueError("loc 1")
        else:
            raise ValueError("loc 2")

    with capture_out() as o:
        with pytest.raises(MultipleFailures):
            test()
    assert "loc 1" in o.getvalue()
    assert "loc 2" in o.getvalue()
Esempio n. 54
0
def test_runs_multiple_cleanup_with_teardown():
    with capture_out() as o:
        with pytest.raises(AssertionError):
            with bc():

                def foo():
                    raise ValueError()

                cleanup(foo)

                def bar():
                    raise TypeError()

                cleanup(foo)
                cleanup(bar)
                raise AssertionError

    assert "ValueError" in o.getvalue()
    assert "TypeError" in o.getvalue()
    assert _current_build_context.value is None
Esempio n. 55
0
def test_prints_all_notes_in_verbose_mode():
    # slightly roundabout because @example messes with verbosity - see #1521
    generated_integers = []

    def notefmt(x):
        return 'x -> %d' % (x,)

    @settings(verbosity=Verbosity.debug)
    @given(integers(1, 10))
    def test(x):
        generated_integers.append(x)
        note(notefmt(x))
        assert x < 5

    with capture_out() as out:
        with reporting.with_reporter(reporting.default):
            with pytest.raises(AssertionError):
                test()
    v = out.getvalue()
    for x in generated_integers:
        assert notefmt(x) in v
Esempio n. 56
0
def test_shrinks_both_failures():
    first_has_failed = [False]
    duds = set()
    second_target = [None]

    @settings(database=None, max_examples=1000)
    @given(st.integers(min_value=0).map(int))
    def test(i):
        if i >= 10000:
            first_has_failed[0] = True
            assert False
        assert i < 10000
        if first_has_failed[0]:
            if second_target[0] is None:
                for j in range(10000):
                    if j not in duds:
                        second_target[0] = j
                        break
            assert i < second_target[0]
        else:
            duds.add(i)

    with capture_out() as o:
        with pytest.raises(MultipleFailures):
            test()

    output = o.getvalue()

    assert_output_contains_failure(
        output,
        test,
        i=10000,
    )

    assert_output_contains_failure(
        output,
        test,
        i=second_target[0],
    )
Esempio n. 57
0
def test_raises_multiple_failures_with_varying_type():
    target = [None]

    @settings(database=None, max_examples=100)
    @given(st.integers())
    def test(i):
        if abs(i) < 1000:
            return
        if target[0] is None:
            # Ensure that we have some space to shrink into, so we can't
            # trigger an minimal example and mask the other exception type.
            assume(1003 < abs(i))
            target[0] = i
        exc_class = TypeError if target[0] == i else ValueError
        raise exc_class()

    with capture_out() as o:
        with pytest.raises(MultipleFailures):
            test()

    assert "TypeError" in o.getvalue()
    assert "ValueError" in o.getvalue()
def test_prints_reproduction_if_requested():
    failing_example = [None]

    @settings(print_blob=True, database=None, max_examples=100)
    @given(st.integers())
    def test(i):
        if failing_example[0] is None and i != 0:
            failing_example[0] = i
        assert i not in failing_example

    with capture_out() as o:
        with pytest.raises(AssertionError):
            test()
    assert "@reproduce_failure" in o.getvalue()

    exp = re.compile(r"reproduce_failure\(([^)]+)\)", re.MULTILINE)
    extract = exp.search(o.getvalue())
    reproduction = eval(extract.group(0))
    test = reproduction(test)

    with pytest.raises(AssertionError):
        test()
def test_when_set_to_no_simplifies_runs_failing_example_twice():
    failing = []

    @given(integers())
    @settings(
        phases=no_shrink,
        max_examples=100,
        verbosity=Verbosity.normal,
        report_multiple_bugs=False,
    )
    def foo(x):
        if x > 11:
            note("Lo")
            failing.append(x)
            assert False

    with raises(AssertionError):
        with capture_out() as out:
            foo()
    assert len(failing) == 2
    assert len(set(failing)) == 1
    assert "Falsifying example" in out.getvalue()
    assert "Lo" in out.getvalue()
Esempio n. 60
0
def test_prints_seed_only_on_healthcheck(
    monkeypatch, in_pytest, fail_healthcheck, verbosity
):
    monkeypatch.setattr(core, "running_under_pytest", in_pytest)

    strategy = st.integers()
    if fail_healthcheck:

        def slow_map(i):
            time.sleep(10)
            return i

        strategy = strategy.map(slow_map)
        expected_exc = FailedHealthCheck
    else:
        expected_exc = AssertionError

    @settings(database=None, verbosity=verbosity)
    @given(strategy)
    def test(i):
        assert fail_healthcheck

    with capture_out() as o:
        with pytest.raises(expected_exc):
            test()

    output = o.getvalue()

    seed = test._hypothesis_internal_use_generated_seed
    assert seed is not None
    if fail_healthcheck and verbosity != Verbosity.quiet:
        assert f"@seed({seed})" in output
        contains_pytest_instruction = f"--hypothesis-seed={seed}" in output
        assert contains_pytest_instruction == in_pytest
    else:
        assert "@seed" not in output
        assert f"--hypothesis-seed={seed}" not in output