-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sim_with_mocks.py
46 lines (36 loc) · 1.44 KB
/
test_sim_with_mocks.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
# pytest and pytest-mock are not explicitly imported but needed for this script
from unittest.mock import Mock, call, ANY
from simulation import whisper, simulate_single_game
def test_whisper(mocker):
close_words = ['from', 'group', 'stop', 'cool']
mock_uniform = Mock(return_value = .6)
mocker.patch('simulation.uniform', mock_uniform)
assert whisper('crop', p_mistake = .5) == 'crop'
mock_uniform.assert_called_once()
mock_uniform = Mock(return_value = .2)
mocker.patch('simulation.uniform', mock_uniform)
assert whisper('crop', p_mistake = .5) in close_words
mock_uniform.assert_called_once()
def test_sim_single_game(mocker):
# Seed: group
# 1st whisper
# uniform() returns 1
# choice not called
# 2nd whisper
# uniform() returns 1
# choice not called
# 3rd whisper
# uniform() returns 0
# choice('group') returns 'grump'
# 4th whisper
# uniform() returns 0
# choice('grump') returns 'goopy'
mock_uniform = Mock(side_effect = [1, 1, 0, 0])
mocker.patch('simulation.uniform', mock_uniform)
mock_choice = Mock(side_effect = ['grump', 'goopy'])
mocker.patch('simulation.choice', mock_choice)
# actual run
game_result = simulate_single_game('group', 4)
assert game_result == ['group', 'group', 'group', 'grump', 'goopy']
mock_uniform.assert_has_calls([call(0,1)]*4)
mock_choice.assert_has_calls([call(ANY)]*2)