-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytest.py
executable file
·73 lines (60 loc) · 2.18 KB
/
mytest.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
#!/usr/bin/python
import unittest
import mock
import mymoc
from mock import patch
class Mytest(unittest.TestCase):
def test_func_main(self):
obj_mocked = mock.Mock()
# obj_mocked.__repr__ = mock.Mock(return_value="55")
print(type(obj_mocked))
# obj_mocked.__repr__ = "asd"
mymoc.fun_called = mock.Mock()
mymoc.fun_main(obj_mocked)
mymoc.fun_called.assert_called_with(obj_mocked, "bb")
# 1> basic unit test
def test_basic_return_pass(self):
x = mymoc.basic_return(2)
self.assertEqual(x, 5)
### Standard import and call
# 2. import method
def test_string_function_import_pass(self):
import string
string.lower = mock.Mock(return_value="i am lower")
string.replace = mock.Mock(return_value="i am lower")
x = mymoc.string_function("I AM UPPER")
# string.lower.assert_called_with(["I AM UPPER"])
self.assertEqual(x, "i am lower")
# 3) as a patch
@patch('mymoc.string.lower')
def test_string_function_patch_pass(self, str_p):
# string.lower = mock.Mock(return_value="eee")
str_p.return_value = "i am lower"
x = mymoc.string_function("I AM UPPER")
self.assertEqual(x, "i am lower")
### From x import y
# 4] import method, FAILS!
def test_random_function_import_pass(self):
return
from random import random
random = mock.Mock(return_value=2000)
x = mymoc.random_function()
self.assertEqual(x, 2000)
# 5- patch method, MUST USE
@patch('mymoc.random')
def test_random_function_patch_pass(self, rand_p):
rand_p.return_value = 2000
x = mymoc.random_function()
self.assertEqual(x, 2000)
def test_host_is_adapter_up(self):
self.host = mymoc.Host()
self.host.adapter.is_up = mock.Mock(return_value="upup")
x = self.host.is_adapter_up1()
self.assertEqual(x, "upup")
def test_host_is_adapter2_up(self):
self.host = mymoc.Host()
# mymoc.Adapter = mock.Mock()
mymoc.Adapter.is_up = mock.Mock(return_value="upup")
self.assertEqual(self.host.is_adapter_up2(), "upup")
if __name__ == "__main__":
unittest.main()