-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtest.py
64 lines (49 loc) · 1.94 KB
/
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
import io
from contextlib import redirect_stdout
from unittest.mock import patch
import unittest
import calculator
class ContainersTestCase(unittest.TestCase):
def setUp(self):
self.response = io.StringIO()
def test_mult(self):
user_input = ["4", "5", "*"]
with redirect_stdout(self.response):
with patch("builtins.input", side_effect=user_input):
calculator.main()
self.assertTrue("20" in self.response.getvalue())
def test_add(self):
user_input = ["4", "5", "+"]
with redirect_stdout(self.response):
with patch("builtins.input", side_effect=user_input):
calculator.main()
self.assertTrue("9" in self.response.getvalue())
def test_div(self):
user_input = ["10", "2", "/"]
with redirect_stdout(self.response):
with patch("builtins.input", side_effect=user_input):
calculator.main()
self.assertTrue("5" in self.response.getvalue())
def test_sub(self):
user_input = ["5", "4", "-"]
with redirect_stdout(self.response):
with patch("builtins.input", side_effect=user_input):
calculator.main()
self.assertTrue("1" in self.response.getvalue())
def test_not_number(self):
user_input = ["5", "a", "-"]
with redirect_stdout(self.response):
with patch("builtins.input", side_effect=user_input):
calculator.main()
self.assertFalse(self.response.getvalue() == "")
def test_invalid_operation(self):
user_input = ["5", "4", "v"]
with redirect_stdout(self.response):
with patch("builtins.input", side_effect=user_input):
calculator.main()
value = self.response.getvalue()
self.assertFalse("1" in value)
self.assertFalse("9" in value)
self.assertFalse("20" in value)
if __name__ == "__main__":
unittest.main()