-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtest_guess.py
84 lines (65 loc) · 1.99 KB
/
test_guess.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
from unittest.mock import patch
import random
import pytest
from guess import get_random_number, Game
@patch.object(random, 'randint')
def test_get_random_number(m):
m.return_value = 17
assert get_random_number() == 17
@patch("builtins.input", side_effect=[11, '12', 'Bob', 12, 5,
-1, 21, 7, None])
def test_guess(inp):
game = Game()
# good
assert game.guess() == 11
assert game.guess() == 12
# not a number
with pytest.raises(ValueError):
game.guess()
# already guessed 12
with pytest.raises(ValueError):
game.guess()
# good
assert game.guess() == 5
# out of range values
with pytest.raises(ValueError):
game.guess()
with pytest.raises(ValueError):
game.guess()
# good
assert game.guess() == 7
# user hit enter
with pytest.raises(ValueError):
game.guess()
def test_validate_guess(capfd):
game = Game()
game._answer = 2
assert not game._validate_guess(1)
out, _ = capfd.readouterr()
assert out.rstrip() == '1 is too low'
assert not game._validate_guess(3)
out, _ = capfd.readouterr()
assert out.rstrip() == '3 is too high'
assert game._validate_guess(2)
out, _ = capfd.readouterr()
assert out.rstrip() == '2 is correct!'
@patch("builtins.input", side_effect=[4, 22, 9, 4, 6])
def test_game_win(inp, capfd):
game = Game()
game._answer = 6
game()
assert game._win is True
out = capfd.readouterr()[0]
expected = ['4 is too low', 'Number not in range',
'9 is too high', 'Already guessed',
'6 is correct!', 'It took you 3 guesses']
output = [line.strip() for line
in out.split('\n') if line.strip()]
for line, exp in zip(output, expected):
assert line == exp
@patch("builtins.input", side_effect=[None, 5, 9, 14, 11, 12])
def test_game_lose(inp, capfd):
game = Game()
game._answer = 13
game()
assert game._win is False