Zappy - Year end project 2
This is a project that Epitech asked us to create in order to allow us to reveiw the notions of the current year.
Loading...
Searching...
No Matches
test_main.py
Go to the documentation of this file.
7"""! @brief Example program for running unit test functions """
8
9import sys
10import unittest
11from unittest import mock
12
13
14def test_function() -> None:
15 """! A test function """
16 print("Test function")
17
18
19def test_function2() -> str:
20 """! A second test function """
21 return "Test function2"
22
23
24def test_function3() -> str:
25 """! A third test function """
26 return input("Test function3:")
27
28
29def test_function4() -> None:
30 """! A fourth test function """
31 print(f"You have entered: {input('Test function4:')}")
32
33
34class TestMainFunctions(unittest.TestCase):
35 """! Test the test functions that are not sure """
36
37 def setUp(self):
38 """! Setup method that is called before each test """
39 self.debug = False
40
41 def print_debug(self, string: str, *args, **kwargs) -> None:
42 """! Print a debug message
43 @param self The self argument
44 @param string The string to be displayed
45 @param *args optional arguments the user whishes to pass
46 @param **kwargs optional arguments the user whishes to pass
47 """
48 if self.debug:
49 print(f"DEBUG: {string}", file=sys.stderr, *args, **kwargs)
50
51 def test_test1(self) -> None:
52 """! Test the test1 function
53 @param self The self argument
54 """
55 self.print_debug("test_test1")
56 self.assertIsNone(test_function())
57
58 def test_test2(self) -> None:
59 """ Test the test2 function
60 @param self The self argument
61 """
62 self.print_debug("test_test2")
63 self.assertEqual(test_function2(), "Test function2")
64
65 def test_test3(self) -> None:
66 """ Test the test3 function
67 @param self The self argument
68 """
69 self.print_debug("test_test3")
70 usr_input = "dadada"
71 with mock.patch("builtins.input", return_value=usr_input):
72 self.assertEqual(test_function3(), usr_input)
73
74 def test_test4(self) -> None:
75 """ Test the test4 function
76 @param self The self argument
77 """
78 self.print_debug("test_test4")
79 usr_input = "dadada"
80 with mock.patch("builtins.input", return_value=usr_input):
81 with mock.patch("builtins.print", return_value=None) as mock_print:
83 mock_print.assert_called_once_with(
84 f"You have entered: {usr_input}")
85
86 def test_test1_mock(self) -> None:
87 """ Test the test1 function with a mock
88 @param self The self argument
89 """
90 self.print_debug("test_test1_mock")
91 with mock.patch("builtins.print", return_value=None) as mock_print:
93 mock_print.assert_called_once_with("Test function")
94
95 def test_test2_mock(self) -> None:
96 """ Test the test2 function with a mock
97 @param self The self argument
98 """
99 self.print_debug("test_test2_mock")
100 with mock.patch("builtins.print", return_value=None) as mock_print:
102 mock_print.assert_not_called()
103
104 def test_test3_mock(self) -> None:
105 """ Test the test3 function with a mock
106 @param self The self argument
107 """
108 self.print_debug("test_test3_mock")
109 usr_input_mock = "dadada"
110 with mock.patch("builtins.input", side_effect=[usr_input_mock]) as mock_input:
111 self.assertEqual(test_function3(), usr_input_mock)
112 mock_input.assert_called_once_with("Test function3:")
113
114 def test_test4_mock(self) -> None:
115 """ Test the test4 function with a mock
116 @param self The self argument
117 """
118 self.print_debug("test_test4_mock")
119 usr_input_mock = "dadada"
120 with mock.patch("builtins.print", return_value=None) as mock_print:
121 with mock.patch("builtins.input", side_effect=[usr_input_mock]) as mock_input:
123 mock_input.assert_called_once_with("Test function4:")
124 mock_print.assert_called_once_with(
125 f"You have entered: {usr_input_mock}")
126
127
128if __name__ == "__main__":
129 print("Running tests for main.py...")
130 unittest.main()
Test the test functions that are not sure.
Definition test_main.py:34
None test_test1(self)
Test the test1 function.
Definition test_main.py:51
setUp(self)
Setup method that is called before each test.
Definition test_main.py:37
None print_debug(self, str string, *args, **kwargs)
Print a debug message.
Definition test_main.py:41
None test_function()
A test function.
Definition test_main.py:14
str test_function3()
A third test function.
Definition test_main.py:24
None test_function4()
A fourth test function.
Definition test_main.py:29
str test_function2()
A second test function.
Definition test_main.py:19