|
| 1 | +import unittest |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from simple_prog import second_API, third_API |
| 5 | + |
| 6 | + |
| 7 | +class Test_API(unittest.TestCase): |
| 8 | + |
| 9 | + # Here is a example of mock.patch used as decorator |
| 10 | + @mock.patch('simple_prog.third_API.get_data', return_value="data3") |
| 11 | + @mock.patch('simple_prog.second_API.get_data', return_value="data2") |
| 12 | + def test_01_mock_patch_decorator(self, represents_closer_patch, represents_farthest_patch): |
| 13 | + self.assertEqual(second_API().get_data(), 'data2') |
| 14 | + self.assertEqual(third_API().get_data(), 'data3') |
| 15 | + |
| 16 | + # We can do the same using the 'with' statement / No argument needed |
| 17 | + def test_02_mock_patch_with_statement(self): |
| 18 | + with mock.patch('simple_prog.second_API.get_data', return_value="data2"): |
| 19 | + with mock.patch('simple_prog.third_API.get_data', return_value="data3"): |
| 20 | + self.assertEqual(second_API().get_data(), 'data2') |
| 21 | + self.assertEqual(third_API().get_data(), 'data3') |
| 22 | + |
| 23 | + # Or separatly |
| 24 | + def test_03_separate_second_API(self): |
| 25 | + with mock.patch('simple_prog.second_API.get_data', return_value="data2"): |
| 26 | + self.assertEqual(second_API().get_data(), 'data2') |
| 27 | + |
| 28 | + def test_04_separate_third_API(self): |
| 29 | + with mock.patch('simple_prog.third_API.get_data', return_value="data3"): |
| 30 | + self.assertEqual(third_API().get_data(), 'data3') |
| 31 | + |
| 32 | + # It is also possible to add setUp and tearDown, respectively executed before or after the test function |
| 33 | + def setUp(self) -> None: |
| 34 | + self.second_API_patch = mock.patch('simple_prog.second_API.get_data', return_value="data2") |
| 35 | + self.third_API_patch = mock.patch('simple_prog.third_API.get_data', return_value="data3") |
| 36 | + self.second_API_patch.start() |
| 37 | + self.third_API_patch.start() |
| 38 | + |
| 39 | + def test_05_with_setup_teardown(self): |
| 40 | + self.assertEqual(second_API().get_data(), 'data2') |
| 41 | + self.assertEqual(third_API().get_data(), 'data3') |
| 42 | + |
| 43 | + def tearDown(self) -> None: |
| 44 | + self.second_API_patch.stop() |
| 45 | + self.third_API_patch.stop() |
| 46 | + |
| 47 | + # A test function can also return different value, depending of the number of calls |
| 48 | + def test_06_side_effects(self): |
| 49 | + with mock.patch('simple_prog.second_API.get_data', side_effect=["data2", "data3", "data4"]): |
| 50 | + # first call should return 'data2' |
| 51 | + self.assertEqual(second_API().get_data(), 'data2') |
| 52 | + # second call should return 'data3' |
| 53 | + self.assertEqual(second_API().get_data(), 'data3') |
| 54 | + # third call should return 'data4' |
| 55 | + self.assertEqual(second_API().get_data(), 'data4') |
| 56 | + |
| 57 | + # This can also be used for Exception calls |
| 58 | + def test_07_mock_patch_exception(self): |
| 59 | + with mock.patch('simple_prog.second_API.get_data', side_effect=Exception("Boom!")) as second_API_patch: |
| 60 | + # self.assertEqual( second_API().get_data(),'data2') #Return 'Exception: Boom!' |
| 61 | + self.assertRaises(Exception, second_API_patch) |
| 62 | + |
| 63 | + # mock.patch can be changed as mock.patch.object to facilitate the reading |
| 64 | + def test_08_with_mock_patch_object(self): |
| 65 | + with mock.patch.object(second_API, 'get_data', return_value="data2"): |
| 66 | + self.assertEqual(second_API().get_data(), 'data2') |
| 67 | + |
| 68 | + |
0 commit comments