forked from doctaphred/phrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop.py
46 lines (36 loc) · 1.06 KB
/
stop.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
from contextlib import contextmanager
from unittest.mock import patch
def stop():
"""Immediately halt execution of the code under test.
Useful for reducing the duration of integration tests and the risk
of side effects from irrelevant code.
Each call to this function returns a unique class which may be
specifically caught, without interfering with other simultaneous
uses.
"""
return type('Stop', (BaseException,), {})
@contextmanager
def stop_at(target):
exc = stop()
with patch(target, side_effect=exc()):
try:
yield
except exc:
pass
@contextmanager
def assert_called(target):
exc = stop()
with patch(target, side_effect=exc()):
try:
yield
except exc:
pass
raise AssertionError('{} was not called'.format(target))
@contextmanager
def assert_not_called(target):
exc = stop()
with patch(target, side_effect=exc()):
try:
yield
except exc:
raise AssertionError('{} was called'.format(target))