File: test_replay_logic.py

package info (click to toggle)
python-hypothesis 6.138.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,272 kB
  • sloc: python: 62,853; ruby: 1,107; sh: 253; makefile: 41; javascript: 6
file content (145 lines) | stat: -rw-r--r-- 3,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import pytest

from hypothesis import given, settings, strategies as st
from hypothesis.database import InMemoryExampleDatabase
from hypothesis.internal.compat import ExceptionGroup


def test_does_not_shrink_on_replay():
    database = InMemoryExampleDatabase()

    call_count = 0

    is_first = True
    last = None

    @settings(
        database=database,
        report_multiple_bugs=False,
        derandomize=False,
        max_examples=1000,
    )
    @given(st.lists(st.integers(), unique=True, min_size=3))
    def test(ls):
        nonlocal call_count, is_first, last
        if is_first and last is not None:
            assert ls == last
        is_first = False
        last = ls
        call_count += 1
        raise AssertionError

    with pytest.raises(AssertionError):
        test()

    assert last is not None

    call_count = 0
    is_first = True

    with pytest.raises(AssertionError):
        test()

    assert call_count == 2


def test_does_not_shrink_on_replay_with_multiple_bugs():
    database = InMemoryExampleDatabase()
    call_count = 0
    raised = False
    marker = 1000093

    @settings(
        database=database,
        report_multiple_bugs=True,
        derandomize=False,
        max_examples=1000,
    )
    @given(st.integers())
    def test(n):
        nonlocal call_count, raised
        call_count += 1
        if n >= marker:
            raised = True
            raise AssertionError
        elif n < marker and raised:
            raise AssertionError

    with pytest.raises(ExceptionGroup):
        test()

    call_count = 0

    with pytest.raises(ExceptionGroup):
        test()

    assert call_count == 4


def test_will_always_shrink_if_previous_example_does_not_replay():
    database = InMemoryExampleDatabase()

    good = set()
    last = None

    @settings(
        database=database,
        report_multiple_bugs=True,
        derandomize=False,
        max_examples=1000,
    )
    @given(st.integers(min_value=0))
    def test(n):
        nonlocal last
        if n not in good:
            last = n
            raise AssertionError

    for i in range(20):
        with pytest.raises(AssertionError):
            test()
        assert last == i
        good.add(last)


def test_will_shrink_if_the_previous_example_does_not_look_right():
    database = InMemoryExampleDatabase()

    last = None

    first_test = True

    @settings(database=database, report_multiple_bugs=True, derandomize=False)
    @given(st.data())
    def test(data):
        nonlocal last
        m = data.draw(st.integers())
        last = m
        if first_test:
            data.draw(st.integers())
            assert m < 10000
        else:
            raise AssertionError

    with pytest.raises(AssertionError):
        test()

    assert last is not None
    assert last > 0

    first_test = False

    with pytest.raises(AssertionError):
        test()

    assert last == 0