Advertisement
a_igin

mock_demo

Nov 3rd, 2020
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. from unittest.mock import patch
  2. from unittest import TestCase, main
  3.  
  4.  
  5. class MockedClass:
  6.  
  7.     def real_method(self):
  8.         print('___')
  9.         print('Реальный метод')
  10.         print('Выполняется реальная работа')
  11.         print('___')
  12.  
  13.         return 'OK'
  14.  
  15.  
  16. class MyTestCase(TestCase):
  17.  
  18.     def test_real_method(self):
  19.         mocked_class = MockedClass()
  20.         result = mocked_class.real_method()
  21.         self.assertEqual(result, 'OK')
  22.  
  23.     @patch('__main__.MockedClass.real_method')
  24.     def test_real_method_mocked(self, mock_real_method):
  25.         mock_real_method.return_value = 'OK'
  26.         mocked_class = MockedClass()
  27.         result = mocked_class.real_method()
  28.         self.assertEqual(result, 'OK')
  29.  
  30.  
  31. main()
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement