def test_reports_each_added_test_class(self): objects = [Class('SomeClass', [Method('some_method')]), Function('some_function')] generate_single_test_module(objects=objects) assert_contains_once(self._get_log_output(), "Adding generated TestSomeClass to %s." % P("tests/test_module.py")) assert_contains_once(self._get_log_output(), "Adding generated TestSomeFunction to %s." % P("tests/test_module.py"))
def test_generates_assert_equal_test_stub_for_functions_which_take_functions_as_arguments(self): objects = [FunctionWithSingleCall('higher', {'f': lambda: 42}, True)] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_higher_returns_true_for_function(self):") assert_contains(result, "# self.assertEqual(True, higher(<TODO: function>))")
def test_generates_nice_name_for_tests_with_init_that_takes_no_arguments(self): klass = ClassWithMethods('Something', [('__init__', [({}, None)]), ('fire', [({}, 'kaboom')])]) result = generate_single_test_module(objects=[klass]) assert_contains(result, "def test_fire_returns_kaboom(self):")
def test_comes_up_with_unique_names_for_each_test_method(self): objects = [FunctionWithCalls('opt', [({'x': 3}, True), ({'y': 3}, True)])] result = generate_single_test_module(objects=objects) assert_contains_one_after_another(result, 'test_opt_returns_true_for_3', 'test_opt_returns_true_for_3_case_2')
def test_sorts_new_test_methods_by_name(self): objects = [FunctionWithCalls('square', [({'x': 2}, 4), ({'x': 3}, 9)])] result = generate_single_test_module(objects=objects) assert_contains_one_after_another(result, 'test_square_returns_4_for_2', 'test_square_returns_9_for_3')
def test_generates_assert_equal_type_test_stub_for_functions_which_take_and_return_functions(self): objects = [FunctionWithSingleCall('highest', {'f': lambda: 42}, lambda: 43)] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_highest_returns_function_for_function(self):") assert_contains(result, "# self.assertEqual(types.FunctionType, type(highest(<TODO: function>)))")
def test_generates_test_case_for_each_function_call_with_numbers(self): objects = [FunctionWithSingleCall('square', {'x': 4}, 16)] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_square_returns_16_for_4(self):") assert_contains(result, "self.assertEqual(16, square(4))")
def test_handles_unicode_objects(self): objects = [FunctionWithSingleCall('characterize', {'x': u'\xf3'}, "o-acute")] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_characterize_returns_oacute_for_unicode_string(self):") assert_contains(result, "self.assertEqual('o-acute', characterize(u'\\xf3'))")
def test_generates_assert_raises_for_generator_functions_with_exceptions(self): objects = [GeneratorWithSingleException('throw', {'string': {}}, TypeError())] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_throw_raises_type_error_for_dict(self):") assert_contains(result, "self.assertRaises(TypeError, lambda: list(islice(throw({}), 1)))")
def test_generates_assert_raises_stub_for_functions_with_string_exceptions(self): function = FunctionWithSingleException('bad_function', {}, "bad error") result = generate_single_test_module(objects=[function]) assert_contains(result, "def test_bad_function_raises_bad_error(self):") assert_contains(result, "# self.assertRaises(<TODO: 'bad error'>, lambda: bad_function())")
def test_generates_assert_raises_stub_for_init_methods_with_exceptions(self): klass = ClassWithMethods('Something', [('__init__', [({'x': lambda: 42}, ValueError())])], 'exception') result = generate_single_test_module(objects=[klass]) assert_contains(result, "def test_creation_with_function_raises_value_error(self):") assert_contains(result, "# self.assertRaises(ValueError, lambda: Something(<TODO: function>))")
def test_generates_assert_equal_for_exception_returned_as_value(self): objects = [FunctionWithSingleCall('error_factory', {}, MemoryError(0, "OOM!"))] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_error_factory_returns_memory_error(self):") assert_contains(result, "self.assertEqual(MemoryError(0, 'OOM!'), error_factory())")
def test_generates_assert_raises_for_functions_with_exceptions(self): function = FunctionWithSingleException('square', {'x': 'hello'}, TypeError()) result = generate_single_test_module(objects=[function]) assert_contains(result, "def test_square_raises_type_error_for_hello(self):") assert_contains(result, "self.assertRaises(TypeError, lambda: square('hello'))")
def test_generates_assert_equal_for_generator_functions(self): objects = [GeneratorWithYields('random', {'seed': 1}, [1, 8, 7, 2])] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_random_yields_1_then_8_then_7_then_2_for_1(self):") assert_contains(result, "self.assertEqual([1, 8, 7, 2], list(islice(random(1), 4)))")
def test_generates_assert_equal_types_for_generator_functions_with_unpickable_outputs(self): objects = [GeneratorWithYields('lambdify', {'x': 1}, [lambda: 1, lambda: 2])] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_lambdify_yields_function_then_function_for_1(self):") assert_contains(result, "self.assertEqual([types.FunctionType, types.FunctionType], map(type, list(islice(lambdify(1), 2))))")
def test_generates_test_case_for_each_function_call_with_strings(self): objects = [FunctionWithSingleCall('underscore', {'name': 'John Smith'}, 'john_smith')] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_underscore_returns_john_smith_for_John_Smith(self):") assert_contains(result, "self.assertEqual('john_smith', underscore('John Smith'))")
def test_generates_assert_equal_stub_for_generator_functions_with_unpickable_inputs(self): objects = [GeneratorWithYields('call_twice', {'x': lambda: 1}, [1, 1])] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_call_twice_yields_1_then_1_for_function(self):") assert_contains(result, "# self.assertEqual([1, 1], list(islice(call_twice(<TODO: function>), 2)))")
def test_generates_assert_raises_test_stub_for_functions_which_take_functions_as_arguments(self): function = FunctionWithSingleException('high', {'f': lambda: 42}, NotImplementedError()) result = generate_single_test_module(objects=[function]) assert_contains(result, "def test_high_raises_not_implemented_error_for_function(self):") assert_contains(result, "# self.assertRaises(NotImplementedError, lambda: high(<TODO: function>))")
def test_generates_sample_assertions_in_test_stubs_for_functions(self): objects = [Function('something', args=['arg1', 'arg2', '*rest'])] result = generate_single_test_module(template='nose', objects=objects) assert_contains(result, "class TestSomething:") assert_contains(result, "# assert_equal(expected, something(arg1, arg2, *rest))") assert_contains(result, "raise SkipTest # TODO: implement your test here")
def test_generates_imports_for_user_defined_exceptions(self): klass = Class("UserDefinedException") function = Function("throw") function.calls = [FunctionCall(function, {}, exception=UserObject(None, klass))] result = generate_single_test_module(objects=[function, klass]) assert_contains(result, "from module import UserDefinedException")
def test_generates_assert_raises_for_normal_methods_with_exceptions(self): klass = ClassWithMethods('Something', [('method', [({}, KeyError())])], 'exception') result = generate_single_test_module(objects=[klass]) assert_contains(result, "def test_method_raises_key_error(self):") assert_contains(result, "something = Something()") assert_contains(result, "self.assertRaises(KeyError, lambda: something.method())")
def test_lists_names_of_tested_methods_in_longer_test_cases(self): klass = ClassWithMethods('Something', [('__init__', [({'arg1': 1, 'arg2': 2}, None)]), ('sum', [({}, 3)]), ('power', [({}, 1)])]) result = generate_single_test_module(objects=[klass]) assert_contains(result, "def test_power_and_sum_after_creation_with_arg1_equal_1_and_arg2_equal_2(self):")
def test_takes_slice_of_generated_values_list_to_work_around_infinite_generators(self): objects = [GeneratorWithYields('nats', {'start': 1}, [1, 2, 3])] result = generate_single_test_module(objects=objects) assert_contains(result, "from itertools import islice") assert_contains(result, "def test_nats_yields_1_then_2_then_3_for_1(self):") assert_contains(result, "self.assertEqual([1, 2, 3], list(islice(nats(1), 3)))")
def test_generates_assert_equal_for_generator_methods(self): klass = ClassWithMethods('SuperGenerator', [('degenerate', [({'what': 'strings'}, ['one', 'two'])])], call_type='generator') result = generate_single_test_module(objects=[klass]) assert_contains(result, "def test_degenerate_yields_one_then_two_for_strings(self):") assert_contains(result, "super_generator = SuperGenerator()") assert_contains(result, "self.assertEqual(['one', 'two'], list(islice(super_generator.degenerate('strings'), 2)))")
def test_handles_environment_error_with_three_arguments(self): objects = [FunctionWithSingleCall('bad_address', {}, EnvironmentError(14, 'Bad address', 'module.py'))] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_bad_address_returns_environment_error(self):") assert_contains(result, "self.assertEqual(EnvironmentError(14, 'Bad address', 'module.py'), bad_address())")
def test_lists_names_of_tested_methods_called_multiple_times_in_longer_test_cases(self): klass = ClassWithMethods('Developer', [('look_at', [({'what': 'bad code'}, 'sad'), ({'what': 'good code'}, 'happy')]), ('modify', [({}, True)])]) result = generate_single_test_module(objects=[klass]) assert_contains(result, "def test_look_at_2_times_and_modify(self):")
def test_handles_environment_error_with_two_arguments(self): objects = [FunctionWithSingleCall('arg_list_too_long', {}, EnvironmentError(7, 'Arg list too long'))] result = generate_single_test_module(objects=objects) assert_contains(result, "def test_arg_list_too_long_returns_environment_error(self):") assert_contains(result, "self.assertEqual(EnvironmentError(7, 'Arg list too long'), arg_list_too_long())")
def test_handles_localizable_function_objects(self): objects = [FunctionWithSingleCall('store', {'fun': read_file_contents}, None)] result = generate_single_test_module(objects=objects) assert_contains(result, "from pythoscope.util import read_file_contents") assert_contains(result, "def test_store_returns_None_for_read_file_contents_function(self):") assert_contains(result, "self.assertEqual(None, store(read_file_contents))")
def test_doesnt_test_unused_generators(self): objects = [GeneratorWithYields('useless', {'anything': 123}, [])] result = generate_single_test_module(objects=objects) assert_contains(result, "assert False") assert_doesnt_contain(result, " self.assertEqual") assert_doesnt_contain(result, " self.assertRaises")
def test_handles_regular_expression_pattern_objects(self): objects = [FunctionWithSingleCall('matches', {'x': re.compile('abcd')}, True)] result = generate_single_test_module(objects=objects) assert_contains(result, "import re") assert_contains(result, "def test_matches_returns_true_for_abcd_pattern(self):") assert_contains(result, "self.assertEqual(True, matches(re.compile('abcd')))")