def test_with_function(self): mock = Mock() function_mock = FunctionMock(mock) sample_loader = Mock() test_generator = sample_test_generator() sample_loader.load_function.return_value = [test_generator] function_mock.loaders = [sample_loader] loader = TestLoader() test_suite = list(loader.load_object(function_mock, FakeModule({}))) # Call test test_suite[0]() # Check loader call self.assertEquals(sample_loader.load_function.call_count, 1) args = sample_loader.load_function.call_args self.assertTrue(isinstance(args[0][0], Test)) # Check test self.assertEqual(mock.call_args, test_generator.args) self.assertEqual(test_generator.set_ups[0].call_count, 1) self.assertEqual(test_generator.tear_downs[0].call_count, 1) self.assertEqual(test_suite[0].messages, test_generator.messages)
def test_with_class_and_test_method(self): fake_test_case = MockTest() fake_module = FakeModule({}) sample_loader = Mock() sample_loader.load_method.return_value = [ sample_test_generator('method_generator') ] sample_loader.load_class.return_value = [ sample_test_generator('class_generator') ] fake_test_case.loaders = [sample_loader] # Simulate method decorator fake_test_case.test_test.im_func.loaders = [sample_loader] loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), 1) test = test_suite[0] test() # Check id self.assertIn('class_generator', test.id)
def test_with_test_method(self): fake_test_case = MockTest() fake_module = FakeModule({}) sample_loader = Mock() test_generator = sample_test_generator() sample_loader.load_method.return_value = [test_generator] # Simulate method decorator fake_test_case.test_test.im_func.loaders = [sample_loader] loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), 1) # Call test test_suite[0]() # Check calls self.assertEqual(fake_test_case.test_mock.call_count, 1) self.assertEqual(fake_test_case.test_mock.call_args, test_generator.args) self.assertEqual(test_generator.set_ups[0].call_count, 1) self.assertEqual(test_generator.tear_downs[0].call_count, 1) self.assertEqual(test_suite[0].messages, test_generator.messages)
def test_simple_function(self): mock = Mock() function_mock = FunctionMock(mock) loader = TestLoader() fake_module = FakeModule({}) test_suite = list(loader.load_object(function_mock, fake_module)) self.assertEquals(len(test_suite), 1) test_suite[0]() self.assertEquals( test_suite[0].id, '%s.%s' % (fake_module.__name__, function_mock.__name__)) self.assertTrue(mock.called)
def test_unittest_case(self): fake_test_case = UnittestCaseMock() loader = UnittestLoader() fake_module = FakeModule({}) test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), 1) test_suite[0]() self.assertEquals( test_suite[0].id, '%s.%s.%s' % (fake_module.__name__, fake_test_case.__name__, 'test_test')) self.assertTrue(fake_test_case.set_up_mock.called) self.assertTrue(fake_test_case.test_mock.called) self.assertTrue(fake_test_case.tear_down_mock.called)
def test_with_class(self): fake_test_case = MockTest() fake_module = FakeModule({}) sample_loader = Mock() test_generator = sample_test_generator() sample_loader.load_class.return_value = [test_generator] fake_test_case.loaders = [sample_loader] loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), 1) self.assertEqual(fake_test_case.init_mock.call_count, 1) self.assertEqual(fake_test_case.init_mock.call_args, test_generator.args)
def test_call_generator_function(self): mock = Mock() function_mock = FunctionMock(mock) arg_name = 'arg' test_args = {'call1': 1, 'call2': 2, 'call3': 3} CallGenerator(arg_name, **test_args)(function_mock) loader = TestLoader() test_suite = list(loader.load_object(function_mock, FakeModule({}))) self.assertEquals(len(test_args), len(test_args)) for test in test_suite: test() expected = [call(**{arg_name: x}) for x in test_args.values()] self.assertEquals(mock.call_args_list, expected)
def test_data_provider_function(self): mock = Mock() function_mock = FunctionMock(mock) tests = {'call1': call(1, arg=2), 'call2': call(2, arg=3), 'call3': call(3, arg=4)} DataProvider(**tests)(function_mock) loader = TestLoader() test_suite = list(loader.load_object(function_mock, FakeModule({}))) self.assertEquals(len(test_suite), len(tests)) for test_number, test in enumerate(test_suite): test() self.assertTrue(test.id.endswith(tests.keys()[test_number])) self.assertEquals(mock.call_args_list, tests.values())
def test_instanciation(self): fake_test_case = MockMultipleTests() loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, FakeModule({}))) self.assertEquals(len(test_suite), 3) for test in test_suite: test() self.assertEquals(fake_test_case.set_up_mock.call_count, 3) self.assertEquals(fake_test_case.test_mock.call_count, 3) self.assertEquals(fake_test_case.tear_down_mock.call_count, 3) # Check that loader created a new instance for each test in order to # avoid edge effect on tests self.assertEquals(len(fake_test_case.instances), 3) self.assertEquals(len(set(fake_test_case.instances)), 3)
def test_data_provider_class(self): fake_test_case = MockTest() fake_module = FakeModule({}) arg_name = 'arg' test_args = {'call1': 1, 'call2': 2, 'call3': 3} CallGenerator(arg_name, **test_args)(fake_test_case) loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), len(test_args)) for test_number, test in enumerate(test_suite): test() self.assertIn(test_args.keys()[test_number], test.id) expected = [call(**{arg_name: x}) for x in test_args.values()] self.assertEqual(fake_test_case.init_mock.call_args_list, expected)
def test_build_test_suite(self): function_mock = FunctionMock(Mock()) fake_module = FakeModule({'test': function_mock}) sys.modules[fake_module.__name__] = fake_module mock_finder = Mock() mock_finder.find.return_value = ['%s.py' % fake_module.__name__] finders = [mock_finder] loader = TestLoader() loaders = [loader] runner = TestRunner(finders, loaders) test_suite = runner._build_test_suite() self.assertEquals(test_suite, [Test('fake_module.call', function_mock)]) del sys.modules[fake_module.__name__]
def test_data_provider_class(self): fake_test_case = MockTest() fake_module = FakeModule({}) tests = {'call1': call(1, arg=2), 'call2': call(2, arg=3), 'call3': call(3, arg=4)} DataProvider(**tests)(fake_test_case) loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), len(tests)) for test_number, test in enumerate(test_suite): test() self.assertIn(tests.keys()[test_number], test.id) self.assertEqual(fake_test_case.init_mock.call_args_list, tests.values())
def test_call_generator_method(self): fake_test_case = MockTest() fake_module = FakeModule({}) arg_name = 'arg' test_args = {'call1': 1, 'call2': 2, 'call3': 3} CallGenerator(arg_name, **test_args)(fake_test_case.__dict__['test_test']) loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), len(test_args)) for test in test_suite: test() self.assertEquals(fake_test_case.set_up_mock.call_count, len(test_args)) expected = [call(**{arg_name: x}) for x in test_args.values()] self.assertEquals(fake_test_case.test_mock.call_args_list, expected) self.assertEquals(fake_test_case.tear_down_mock.call_count, len(test_args))
def test_data_provider_method(self): fake_test_case = MockTest() fake_module = FakeModule({}) tests = {'call1': call(1, arg=2), 'call2': call(2, arg=3), 'call3': call(3, arg=4)} DataProvider(**tests)(fake_test_case.__dict__['test_test']) loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), len(tests)) for test_number, test in enumerate(test_suite): test() self.assertTrue(test.id.endswith(tests.keys()[test_number])) self.assertEquals(fake_test_case.set_up_mock.call_count, len(tests)) self.assertEquals(fake_test_case.test_mock.call_args_list, tests.values()) self.assertEquals(fake_test_case.tear_down_mock.call_count, len(tests))
def test_data_provider_function_messages(self): mock = Mock() function_mock = FunctionMock(mock) tests = {'call1': call(1, arg=2), 'call2': call(2, arg=3), 'call3': call(3, arg=4)} DataProvider(**tests)(function_mock) loader = TestLoader() test_suite = list(loader.load_object(function_mock, FakeModule({}))) self.assertEquals(len(test_suite), len(tests)) # TODO check order for test_number, test in enumerate(test_suite): test_call = tests.values()[test_number] message = test.messages[0][1] self.assertIn(str(test_call), message) self.assertIn('Function', message) test() self.assertEquals(mock.call_args_list, tests.values())
def test_data_provider_method_messages(self): fake_test_case = MockTest() fake_module = FakeModule({}) tests = {'call1': call(1, arg=2), 'call2': call(2, arg=3), 'call3': call(3, arg=4)} DataProvider(**tests)(fake_test_case.__dict__['test_test']) loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), len(tests)) # TODO check order for test_number, test in enumerate(test_suite): test_call = tests.values()[test_number] message = test.messages[0][1] self.assertIn(str(test_call), message) self.assertIn('Method', message) test() self.assertEquals(fake_test_case.set_up_mock.call_count, len(tests)) self.assertEquals(fake_test_case.test_mock.call_args_list, tests.values()) self.assertEquals(fake_test_case.tear_down_mock.call_count, len(tests))
def test_data_provider_class_and_method(self): fake_test_case = MockTest() fake_module = FakeModule({}) class_tests = {'call1': call(1, arg=2), 'call2': call(2, arg=3), 'call3': call(3, arg=4)} DataProvider(**class_tests)(fake_test_case) method_tests = {'call4': call(4, arg=5), 'call5': call(5, arg=6), 'call6': call(6, arg=7)} DataProvider(**method_tests)(fake_test_case.__dict__['test_test']) loader = TestLoader() test_suite = list(loader.load_object(fake_test_case, fake_module)) self.assertEquals(len(test_suite), len(class_tests) * len(method_tests)) for test_number, test in enumerate(test_suite): test() self.assertIn(class_tests.keys()[test_number/3], test.id) self.assertTrue(test.id.endswith(method_tests.keys()[(test_number%3)])) self.assertEqual(fake_test_case.init_mock.call_args_list, class_tests.values())
def setUp(self): self.module_object = sentinel.OBJECT self.module = FakeModule({'test': self.module_object})