How to use test_mock method in pyshould

Best Python code snippet using pyshould_python

test_mock.py

Source:test_mock.py Github

copy

Full Screen

1import unittest2from mock import create_mock, Mock3class TestClass():4 def method1(self, param1, param2):5 pass6 def method2(self, arg1, arg2):7 pass8class MockTestCase(unittest.TestCase):9 def test_should_create_mock(self):10 test_mock = create_mock()11 self.assertIsNotNone(test_mock, msg='test mock is None')12 self.assertIsInstance(test_mock, Mock)13 def test_should_create_mock_for_class(self):14 test_mock = create_mock(TestClass)15 self.assertIsNotNone(test_mock)16 self.assertIsInstance(test_mock, Mock)17 def test_should_return_None_by_default(self):18 test_mock = create_mock(TestClass)19 self.assertIsNone(test_mock.method1(1, "hi!"))20 def test_should_return_12(self):21 test_mock = create_mock(TestClass)22 test_mock.when('method1', ('a', 'b'), return_value=12)23 self.assertEqual(test_mock.method1('a', 'b'), 12)24 self.assertIsNone(test_mock.method1(12))25 def test_should_return_hi(self):26 test_mock = create_mock(TestClass)27 test_mock.when('method1', {'param1': 'iweyr', 'param2': 123}, return_value='hi')28 self.assertEqual(test_mock.method1(param1='iweyr', param2=123), 'hi')29 self.assertIsNone(test_mock.method1('ok'))30 def test_should_return_True_always(self):31 test_mock = create_mock(TestClass)32 test_mock.when('method1', return_value=True)33 self.assertEqual(test_mock.method1(param1='iweyr', param2=123), True)34 self.assertEqual(test_mock.method1('ok'), True)35 self.assertEqual(test_mock.method1(), True)36 self.assertIsNone(test_mock.method2())37 def test_should_raise_OSError(self):38 test_mock = create_mock(TestClass)39 test_mock.when('method1', raise_exception=OSError)40 with self.assertRaises(OSError): test_mock.method1('a', 2)41 def test_should_allow_default_ret_value_and_specific_ret_value(self):42 test_mock = create_mock(TestClass)43 test_mock.when('method1', return_value=36)44 test_mock.when('method1', ('a', 7), return_value=11)45 self.assertEqual(test_mock.method1(param1='iweyr', param2=123), 36)46 self.assertEqual(test_mock.method1('ok'), 36)47 self.assertEqual(test_mock.method1('a', 7), 11)48 self.assertEqual(test_mock.method1('a', 9), 36)49 def test_should_raise_OSError_or_not_depending_on_arguments(self):50 test_mock = create_mock(TestClass)51 test_mock.when('method1', ('a', 2), raise_exception=OSError)52 test_mock.when('method1', ('b', 'c'), return_value=12)53 test_mock.when('method1', return_value='hi')54 with self.assertRaises(OSError): test_mock.method1('a', 2)55 self.assertEqual(test_mock.method1('b', 'c'), 12)56 self.assertEqual(test_mock.method1(1,1), 'hi')57 def test_should_have_been_called(self):58 test_mock = create_mock(TestClass)59 self.assertFalse(test_mock.has_been_called())60 test_mock.method1()61 self.assertTrue(test_mock.has_been_called())62 def test_should_reset_mock(self):63 test_mock = create_mock(TestClass)64 test_mock.when('method1', return_value=True)65 self.assertTrue(test_mock.method1())66 self.assertTrue(test_mock.has_been_called())67 test_mock.reset()68 self.assertFalse(test_mock.has_been_called())69 self.assertIsNone(test_mock.method1())70 def test_should_count_times_has_been_called(self):71 test_mock = create_mock(TestClass)72 self.assertFalse(test_mock.has_been_called())73 test_mock.method1()74 self.assertTrue(test_mock.has_been_called())75 test_mock.method2()76 self.assertTrue(test_mock.has_been_called(times=2))77 test_mock.method1('a', 'b')78 self.assertTrue(test_mock.has_been_called(times=3))79 def test_should_count_times_has_been_called_each_method(self):80 test_mock = create_mock(TestClass)81 self.assertFalse(test_mock.has_been_called())82 test_mock.method1()83 self.assertTrue(test_mock.has_been_called(method='method1', times=1))84 test_mock.method2()85 self.assertTrue(test_mock.has_been_called(method='method2', times=1))86 test_mock.method1('a', 'b')87 self.assertTrue(test_mock.has_been_called(method='method1', times=2))88 def test_should_count_times_has_been_called_each_method_with_parameters(self):89 test_mock = create_mock(TestClass)90 self.assertFalse(test_mock.has_been_called())91 test_mock.method1()92 test_mock.method1()93 self.assertTrue(test_mock.has_been_called_with('method1', ()))94 self.assertTrue(test_mock.has_been_called_with('method1', (), times=2))95 self.assertFalse(test_mock.has_been_called_with('method1', (), times=3))96 test_mock.method2('a')97 self.assertTrue(test_mock.has_been_called_with('method2', ('a',)))98 test_mock.method1('a', 'b')99 self.assertTrue(test_mock.has_been_called_with('method1', ('a', 'b'), times=1))100 self.assertFalse(test_mock.has_been_called_with('method1', ('a', 'c'), times=0))101 test_mock.method2(arg2=0, arg1='1')102 self.assertTrue(test_mock.has_been_called_with('method2', {'arg1': '1', 'arg2': 0}))103 self.assertTrue(test_mock.has_been_called_with('method2', {'arg1': '1', 'arg2': 0}, times=1))104 self.assertFalse(test_mock.has_been_called_with('method2', {'arg1': '1', 'arg2': 1}))105if __name__ == '__main__':...

Full Screen

Full Screen

test.py

Source:test.py Github

copy

Full Screen

1import unittest2from unittest import TestCase, mock, main3from unittest.case import expectedFailure4from barky import get_option_choice, get_new_bookmark_info, clear_screen, print_options, get_user_input, \5 get_new_bookmark_data, get_bookmark_id_for_deletion, get_github_import_options, loop67from commands import CreateBookmarksTableCommand89101112"""13 Test carried out here are see if the function are behaving the way they 14 are program to do, by using unittest mock, we are runing function to15 test against each other and see if the behaviour is the same and when test16 against another function should fail1718"""1920class Test_User_Option_Choice(TestCase):2122 @mock.patch('barky.get_option_choice')23 def test_options(self, test_mock):24 test_mock = get_option_choice25 assert test_mock is get_option_choice26 2728 29 @mock.patch('barky.clear_screen')30 def test_clear_screen(self, test_mock):31 test_mock = clear_screen32 assert test_mock is clear_screen333435 @mock.patch('barky.print_options')36 def test_print_options(self, test_mock):37 test_mock = print_options38 assert test_mock is print_options394041 @mock.patch('barky.get_user_input')42 def test_get_user_input(self, test_mock):43 test_mock = get_user_input44 assert test_mock is get_user_input454647 @mock.patch('barky.get_new_bookmark_data')48 def test_get_new_bookmark_data(self, test_mock):49 test_mock = get_new_bookmark_data50 assert test_mock is get_new_bookmark_data515253 @mock.patch('barky.get_bookmark_id_for_deletion')54 def test_get_bookmark_id_for_deletion(self, test_mock):55 test_mock = get_bookmark_id_for_deletion56 assert test_mock is get_bookmark_id_for_deletion575859 @mock.patch('barky.get_github_import_options')60 def test_get_github_import_options(self, test_mock):61 test_mock = get_github_import_options62 assert test_mock is get_github_import_options636465 @mock.patch('barky.get_new_bookmark_info')66 def test_get_new_bookmark_info(self, test_mock):67 test_mock = get_new_bookmark_info68 assert test_mock is get_new_bookmark_info697071 @mock.patch('barky.loop')72 def test_loop(self, test_mock):73 test_mock = loop74 assert test_mock is loop75767778if __name__ == '__main__':79 unittest.main() ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pyshould automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful