-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmock.py
More file actions
73 lines (53 loc) · 2.11 KB
/
mock.py
File metadata and controls
73 lines (53 loc) · 2.11 KB
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
from unittest.mock import MagicMock
from unittest.mock import patch
# all this from https://docs.python.org/3/library/unittest.mock.html
# ----------------------------------------------------------------------
# first, just overwrite the underlying input() method, saving the
# original for later
save_input = __builtins__.input
__builtins__.input = MagicMock(return_value='world')
answer = input()
print("Hello " + answer + "!")
# restore the original
__builtins__.input = save_input
# ----------------------------------------------------------------------
# now, use patch that automatically saves and restores
mock = MagicMock(return_value="whirled")
with patch('builtins.input', mock):
answer = input()
print("Hello " + answer + "!")
# ----------------------------------------------------------------------
# do the same for print
save_print = __builtins__.print
__builtins__.print = MagicMock()
print("Hello world!")
# make sure it was called with the expected value and restore
__builtins__.print.assert_called_once_with("Hello world!")
__builtins__.print = save_print
# ----------------------------------------------------------------------
# and patch for print
mock = MagicMock()
with patch('builtins.print', mock):
print("Hello world!")
mock.assert_called_once_with("Hello world!")
# ----------------------------------------------------------------------
# now, combine the two to do I/O
input_mock = MagicMock(return_value="whirled")
output_mock = MagicMock()
with patch('builtins.input', input_mock):
with patch('builtins.print', output_mock):
answer = input()
print("Hello " + answer + "!")
output_mock.assert_called_once_with("Hello whirled!")
# ----------------------------------------------------------------------
# now use a annotation/decorator for a method. this is probably how you
# want to structure your unit tests
input_mock.reset_mock()
output_mock.reset_mock()
@patch('builtins.input', input_mock)
@patch('builtins.print', output_mock)
def foo():
answer = input()
print("Hello " + answer + "!")
output_mock.assert_called_once_with("Hello whirled!")
foo()