-
Notifications
You must be signed in to change notification settings - Fork 0
/
jazz_test.py
419 lines (290 loc) · 9.2 KB
/
jazz_test.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""Tests for pyJazz."""
import cStringIO
import jazz
import mock
import sys
import unittest
def the_spanish_inquisition():
"""Because one should always expect it."""
return 42
class SuiteRunnerTest(unittest.TestCase):
def setUp(self):
reload(jazz)
self.stdout_bak = sys.stdout
sys.stdout = cStringIO.StringIO()
jazz.VERBOSITY = 9
def tearDown(self):
sys.stdout = self.stdout_bak
@property
def output(self):
return sys.stdout.getvalue()
def test_unasserted_expectations_are_bad(self):
class TheTestClass(jazz.Describe):
def it_should_hate_this(self):
jazz.expect(the_spanish_inquisition())
self.assertRaisesRegexp(SystemExit, '1', jazz.run)
out = self.output
self.assertIn('The Test Class should hate this.', out)
self.assertIn('UnassertedExpectation(', out)
def test_xcluded_tests_do_not_run(self):
it_ran = []
class TheTestClass(jazz.Describe):
def it_should_run_this(self):
it_ran.append(1)
def xit_should_not_run_this(self):
it_ran.append(2)
jazz.run()
self.assertEqual([1], it_ran)
def test_solo_tests_only_run(self):
it_ran = []
class TheTestClass(jazz.Describe):
def iit_should_run_this(self):
it_ran.append(1)
def it_should_not_run_this(self):
it_ran.append(2)
jazz.run()
self.assertEqual([1], it_ran)
def test_nested_suites_run(self):
it_ran = []
class TheTestClass(jazz.Describe):
class SubTestClass(jazz.Describe):
def it_should_run_this(self):
it_ran.append(2)
def it_should_run_this(self):
it_ran.append(1)
jazz.run()
self.assertEqual([1, 2], it_ran)
def test_nested_suites_with_same_name(self):
it_ran = []
class TheTestClass(jazz.Describe):
class TheTestClass(jazz.Describe):
def it_should_run_this_too(self):
it_ran.append(2)
def it_should_run_this(self):
it_ran.append(1)
jazz.run()
out = self.output
self.assertIn('The Test Class should run this.', out)
self.assertIn('The Test Class > The Test Class should run this too.', out)
self.assertListEqual([1, 2], it_ran)
def test_nested_suite_in_x_does_not_run(self):
it_ran = []
class ExcludedTest(jazz.xDescribe):
class NestedNormalTest(jazz.Describe):
def it_should_not_run_this(self):
it_ran.append(2)
class NestedSoloTest(jazz.DDescribe):
def it_should_run_this(self):
it_ran.append(3)
def it_should_not_run_this(self):
it_ran.append(1)
jazz.run()
self.assertEqual([3], it_ran)
def test_before_eaches_run(self):
it_ran = []
class TheTestClass(jazz.Describe):
def before_each(self):
it_ran.append(1)
class SubTestClass(jazz.Describe):
def before_each(self):
it_ran.append(2)
def it_one(self): pass
def it_two(self): pass
def it_one(self): pass
def it_two(self): pass
jazz.run()
expected = [1, 1, 1, 2, 1, 2]
self.assertEqual(expected, it_ran)
def test_after_eaches_run(self):
it_ran = []
class TheTestClass(jazz.Describe):
def after_each(self):
it_ran.append(1)
class SubTestClass(jazz.Describe):
def after_each(self):
it_ran.append(2)
def it_one(self): pass
def it_two(self): pass
def it_one(self): pass
def it_two(self): pass
jazz.run()
expected = [1, 1, 1, 2, 1, 2]
self.assertEqual(expected, it_ran)
class CustomMatchersTest(unittest.TestCase):
def setUp(self):
reload(jazz)
def test_add_matcher(self):
def BeFoo(a): return True
def be_bar(a): return True
jazz.add_matcher(BeFoo)
jazz.add_matcher(be_bar)
jazz.expect(jazz).toBeFoo()
jazz.expect(jazz).toBeBar()
def test_add_matchers(self):
def BeFoo(a): return True
def be_bar(a): return True
matchers = {'be baz': lambda x: True}
jazz.add_matchers(matchers)
jazz.add_matchers([BeFoo, be_bar])
jazz.expect(jazz).toBeFoo()
jazz.expect(jazz).toBeBar()
jazz.expect(jazz).toBeBaz()
def test_custom_matcher(self):
def BeOneMoreThan(a, e):
return e + 1 == a
jazz.add_matcher(BeOneMoreThan)
a = 4
e = 3
jazz.expect(a).toBeOneMoreThan(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeOneMoreThan(e)
class SpyTest(unittest.TestCase):
def test_spy_records(self):
spy = jazz.create_spy('foo')
spy(123)
jazz.expect(spy).to_have_been_called_with(123)
def test_spy_has_no_attributes(self):
spy = jazz.create_spy('foo')
with self.assertRaises(AttributeError):
spy.foo()
def test_spy_cannot_be_chained(self):
spy = jazz.create_spy('foo')
with self.assertRaises(AttributeError):
spy().foo()
def test_spy_obj_records(self):
spy = jazz.create_spy_obj('foo', ['baz', 'cat'])
spy.baz(456)
jazz.expect(spy.baz).to_have_been_called_with(456)
def test_spy_obj_methods_are_restricted(self):
spy = jazz.create_spy_obj('foo', ['baz'])
with self.assertRaises(AttributeError):
spy.bar()
def test_spy_obj_methods_cannot_be_chained(self):
spy = jazz.create_spy_obj('foo', ['baz'])
with self.assertRaises(AttributeError):
spy.baz().bar()
class ExpectationTest(unittest.TestCase):
def test_stringification(self):
string = str(jazz.expect(the_spanish_inquisition()))
self.assertIn('expect(42)', string)
self.assertRegexpMatches(string, r'jazz_test.py:\d+')
self.assertIn(
'test_stringification:string = ' +
'str(jazz.expect(the_spanish_inquisition()))', string)
def test_matchers_can_be_chained(self):
(jazz.expect(3)
.toBe(3)
.notToBeLessThan(3)
.andNotToBeGreaterThan(3)
.toBe(3)
.andNotToBe(4)
.notToBe(2)
.andNotToBe(5))
def test_extra_args(self):
def BeXMoreThan(a, e, x):
return e + x == a
jazz.add_matcher(BeXMoreThan)
a = 5
e = 3
x = 5 - 3
jazz.expect(a).toBeXMoreThan(e, x)
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeXMoreThan(e, x)
def test_expectation_pep8(self):
jazz.expect(True).toBeTruthy()
jazz.expect(True).to_be_truthy()
with self.assertRaises(AssertionError):
jazz.expect(True).notToBeTruthy()
with self.assertRaises(AssertionError):
jazz.expect(True).not_to_be_truthy()
def test_expectation_be(self):
a = {}
e = a
jazz.expect(a).toBe(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToBe(e)
def test_expectation_be_close_to(self):
import math
a = math.pi
e = 3.1415
jazz.expect(a).toBeCloseTo(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeCloseTo(e)
with self.assertRaises(AssertionError):
jazz.expect(a).toBeCloseTo(e, 8)
def test_expectation_be_falsy(self):
a = []
jazz.expect(a).toBeFalsy()
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeFalsy()
def test_expectation_be_greater_than(self):
a = 3
e = 2
jazz.expect(a).toBeGreaterThan(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeGreaterThan(e)
def test_expectation_be_instance_of(self):
a = 3
e = int
jazz.expect(a).toBeInstanceOf(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeInstanceOf(e)
def test_expectation_be_less_than(self):
a = 2
e = 3
jazz.expect(a).toBeLessThan(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeLessThan(e)
def test_expectation_be_none(self):
a = None
jazz.expect(a).toBeNone()
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeNone()
def test_expectation_be_truthy(self):
a = 'truthy string is truthy'
jazz.expect(a).toBeTruthy()
with self.assertRaises(AssertionError):
jazz.expect(a).notToBeTruthy()
def test_expectation_contain(self):
a = ['key']
e = 'key'
jazz.expect(a).toContain(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToContain(e)
def test_expectation_equal(self):
a = 4
e = 4
jazz.expect(a).toEqual(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToEqual(e)
def test_expectation_match(self):
a = 'some string here matches'
e = r'.*matches'
jazz.expect(a).toMatch(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToMatch(e)
def test_expectation_raise(self):
def a(): raise ValueError()
e = ValueError
jazz.expect(a).toRaise(e)
with self.assertRaises(AssertionError):
jazz.expect(a).notToRaise(e)
def a(): raise
jazz.expect(a).toRaise()
with self.assertRaises(AssertionError):
jazz.expect(a).notToRaise()
def test_expectation_been_called(self):
m = mock.Mock()
jazz.expect(m).notToHaveBeenCalled()
m()
jazz.expect(m).toHaveBeenCalled()
def test_expectation_been_called_with(self):
m = mock.Mock()
m(123, bar=45)
jazz.expect(m).toHaveBeenCalledWith(123, bar=45)
def test_expectation_have_length(self):
jazz.expect([1]).toHaveLength(1)
with self.assertRaises(AssertionError):
jazz.expect([1, 2]).toHaveLength(3)
if __name__ == '__main__':
unittest.main()