/
test_explicit_examples.py
234 lines (176 loc) · 5.81 KB
/
test_explicit_examples.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis-python
#
# Most of this work is copyright (C) 2013-2017 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# 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 http://mozilla.org/MPL/2.0/.
#
# END HEADER
from __future__ import division, print_function, absolute_import
from unittest import TestCase
import pytest
from hypothesis import Verbosity, note, given, example, settings, reporting
from hypothesis.errors import InvalidArgument
from tests.common.utils import capture_out
from hypothesis.strategies import text, integers
from hypothesis.internal.compat import integer_types, print_unicode
class TestInstanceMethods(TestCase):
@given(integers())
@example(1)
def test_hi_1(self, x):
assert isinstance(x, integer_types)
@given(integers())
@example(x=1)
def test_hi_2(self, x):
assert isinstance(x, integer_types)
@given(x=integers())
@example(x=1)
def test_hi_3(self, x):
assert isinstance(x, integer_types)
def test_kwarg_example_on_testcase():
class Stuff(TestCase):
@given(integers())
@example(x=1)
def test_hi(self, x):
assert isinstance(x, integer_types)
Stuff(u'test_hi').test_hi()
def test_errors_when_run_with_not_enough_args():
@given(integers(), integers())
@example(1)
def foo(x, y):
pass
with pytest.raises(TypeError):
foo()
def test_errors_when_run_with_not_enough_kwargs():
@given(integers(), integers())
@example(x=1)
def foo(x, y):
pass
with pytest.raises(TypeError):
foo()
def test_can_use_examples_after_given():
long_str = u"This is a very long string that you've no chance of hitting"
@example(long_str)
@given(text())
def test_not_long_str(x):
assert x != long_str
with pytest.raises(AssertionError):
test_not_long_str()
def test_can_use_examples_before_given():
long_str = u"This is a very long string that you've no chance of hitting"
@given(text())
@example(long_str)
def test_not_long_str(x):
assert x != long_str
with pytest.raises(AssertionError):
test_not_long_str()
def test_can_use_examples_around_given():
long_str = u"This is a very long string that you've no chance of hitting"
short_str = u'Still no chance'
seen = []
@example(short_str)
@given(text())
@example(long_str)
def test_not_long_str(x):
seen.append(x)
test_not_long_str()
assert set(seen[:2]) == set((long_str, short_str))
@pytest.mark.parametrize((u'x', u'y'), [(1, False), (2, True)])
@example(z=10)
@given(z=integers())
def test_is_a_thing(x, y, z):
pass
def test_no_args_and_kwargs():
with pytest.raises(InvalidArgument):
example(1, y=2)
def test_no_empty_examples():
with pytest.raises(InvalidArgument):
example()
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
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
def test_prints_verbose_output_for_explicit_examples():
@settings(verbosity=Verbosity.verbose)
@example('NOT AN INTEGER')
@given(integers())
def test_always_passes(x):
pass
with reporting.with_reporter(reporting.default):
with capture_out() as out:
test_always_passes()
out = out.getvalue()
assert u"Trying example: test_always_passes(x='NOT AN INTEGER')" in out
def test_captures_original_repr_of_example():
@example(x=[])
@given(integers())
def test_mutation(x):
x.append(1)
assert not x
with reporting.with_reporter(reporting.default):
with pytest.raises(AssertionError):
with capture_out() as out:
test_mutation()
out = out.getvalue()
assert u'Falsifying example: test_mutation(x=[])' in out
def test_examples_are_tried_in_order():
@example(x=1)
@example(x=2)
@given(integers())
@settings(max_examples=0)
@example(x=3)
def test(x):
print_unicode(u"x -> %d" % (x,))
with capture_out() as out:
with reporting.with_reporter(reporting.default):
test()
ls = out.getvalue().splitlines()
assert ls == [u"x -> 1", 'x -> 2', 'x -> 3']
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
def test_must_agree_with_number_of_arguments():
@example(1, 2)
@given(integers())
def test(a):
pass
with pytest.raises(InvalidArgument):
test()