def test_instance_is_accesible_from_the_moment_it_is_created(self): project = EmptyProject() mod = Module(project=project, subpath='module.py') ct = CodeTree(None) project.remember_code_tree(ct, mod) assert_equal(ct, CodeTree.of(mod))
def _create_project_with_two_points_of_entry(self, *objs): project = EmptyProject() project.create_module("module.py", code=EmptyCode(), objects=list(objs)) self.first = PointOfEntry(project, 'first') self.second = PointOfEntry(project, 'second')
class TestModule: def setUp(self): self.project = EmptyProject() self.module = self.project.create_module("module.py", code=parse("# only comments")) self.test_class = TestClass(name="TestSomething", code=parse("# some test code")) def test_can_add_test_cases_to_empty_modules(self): add_test_case(self.module, self.test_class) # Make sure it doesn't raise any exceptions. def test_adding_a_test_case_adds_it_to_list_of_objects(self): add_test_case(self.module, self.test_class) assert_equal([self.test_class], self.module.objects) def test_test_cases_can_be_added_using_add_objects_method(self): test_class_1 = TestClass(name="TestSomethingElse") test_class_2 = TestClass(name="TestSomethingCompletelyDifferent") self.module.add_objects([test_class_1, test_class_2]) assert_equal([test_class_1, test_class_2], self.module.objects) assert_equal([test_class_1, test_class_2], self.module.test_cases) def test_module_with_errors_doesnt_get_a_code_tree(self): module = self.project.create_module("module_with_errors.py", errors=[Exception()]) assert_raises(CodeTreeNotFound, lambda: CodeTree.of(module))
def setUp(self): CapturedLogger.setUp(self) self.project = EmptyProject() self.existing_test_class = create(TestClass, name="TestSomething") self.test_module = self.project.create_module("test_module.py", code=EmptyCode()) add_test_case(self.test_module, self.existing_test_class)
def test_ignores_modules_with_inspection_errors(self): project = EmptyProject() project.create_module("ugly.py", errors=[Exception()]) tg = TestGenerator() tg.add_tests_to_project(project, ["ugly.py"]) assert_doesnt_contain(self._get_log_output(), "Generating tests for module ugly.py.")
def test_should_not_touch_modules_with_errors(self): project = EmptyProject() module = project.create_module("module.py") test_module = project.create_module("test_module.py", errors=[Exception()]) add_test_case_to_project( project, create(TestClass, associated_modules=[module])) assert test_module.changed is False
def test_should_not_touch_modules_with_errors(self): project = EmptyProject() module = project.create_module("module.py") test_module = project.create_module("test_module.py", errors=[Exception()]) add_test_case_to_project(project, create(TestClass, associated_modules=[module])) assert test_module.changed is False
def test_should_emit_warning_when_trying_to_add_test_to_module_with_errors(self): project = EmptyProject() module = project.create_module("module.py") project.create_module("test_module.py", errors=[Exception()]) add_test_case_to_project(project, create(TestClass, name="FooTest", associated_modules=[module])) assert_contains_once(self._get_log_output(), "WARNING: Not adding FooTest to test_module.py, because " "of a failed inspection.")
def test_replacing_a_test_case_removes_it_from_the_list_of_objects_and_list_of_test_cases(self): project = EmptyProject() module = project.create_module("module.py", code=parse("# only comments")) test_class = create(TestClass, name="TestSomething") new_test_class = create(TestClass, name="TestSomethingElse") add_test_case(module, test_class) replace_test_case(module, test_class, new_test_class) assert_equal([new_test_class], module.objects) assert_equal([new_test_class], module.test_cases)
def test_should_emit_warning_when_trying_to_add_test_to_module_with_errors( self): project = EmptyProject() module = project.create_module("module.py") project.create_module("test_module.py", errors=[Exception()]) add_test_case_to_project( project, create(TestClass, name="FooTest", associated_modules=[module])) assert_contains_once( self._get_log_output(), "WARNING: Not adding FooTest to test_module.py, because " "of a failed inspection.")
def test_replacing_a_test_case_removes_it_from_the_list_of_objects_and_list_of_test_cases( self): project = EmptyProject() module = project.create_module("module.py", code=parse("# only comments")) test_class = create(TestClass, name="TestSomething") new_test_class = create(TestClass, name="TestSomethingElse") add_test_case(module, test_class) replace_test_case(module, test_class, new_test_class) assert_equal([new_test_class], module.objects) assert_equal([new_test_class], module.test_cases)
class TestGeneratorAdderOnCode: def setUp(self): self.generator = UnittestTestGenerator() self.project = EmptyProject() self.module = self.project.create_module("module.py") self.test_module = self.project.create_module("test_module.py") def _test_module_from_code(self, code): return inspect_code(self.project, "test_module.py", code) def _test_class_from_code(self, code, name, method): return self.generator._generate_test_class( name, [TestMethodDescription(method, self.generator.template)], self.module, code) def test_appends_new_test_methods_to_test_classes_with_proper_indentation( self): module = self._test_module_from_code( "class NewTestClass(unittest.TestCase):\n"\ " def test_some_method(self):\n"\ " assert False # c'mon, implement me\n") klass = self._test_class_from_code( "class NewTestClass(unittest.TestCase):\n"\ " def test_new_method(self):\n"\ " assert True # ha!\n", "NewTestClass", "test_new_method") expected_output = "class NewTestClass(unittest.TestCase):\n"\ " def test_some_method(self):\n"\ " assert False # c'mon, implement me\n\n"\ " def test_new_method(self):\n"\ " assert True # ha!\n" add_test_case_to_project(self.project, klass) assert_equal_strings(expected_output, module.get_content()) def test_keeps_future_imports_first(self): module = self._test_module_from_code( "from __future__ import division\n") self.generator.ensure_import(('nose', 'SkipTest')) klass = self._test_class_from_code("", "TestClass", "test_method") add_test_case_to_project(self.project, klass) assert_matches( r"from __future__ import division.*from nose import SkipTest", module.get_content())
class TestGeneratorAdderOnCode: def setUp(self): self.generator = UnittestTestGenerator() self.project = EmptyProject() self.module = self.project.create_module("module.py") self.test_module = self.project.create_module("test_module.py") def _test_module_from_code(self, code): return inspect_code(self.project, "test_module.py", code) def _test_class_from_code(self, code, name, method): return self.generator._generate_test_class(name, [TestMethodDescription(method, self.generator.template)], self.module, code) def test_appends_new_test_methods_to_test_classes_with_proper_indentation(self): module = self._test_module_from_code( "class NewTestClass(unittest.TestCase):\n"\ " def test_some_method(self):\n"\ " assert False # c'mon, implement me\n") klass = self._test_class_from_code( "class NewTestClass(unittest.TestCase):\n"\ " def test_new_method(self):\n"\ " assert True # ha!\n", "NewTestClass", "test_new_method") expected_output = "class NewTestClass(unittest.TestCase):\n"\ " def test_some_method(self):\n"\ " assert False # c'mon, implement me\n\n"\ " def test_new_method(self):\n"\ " assert True # ha!\n" add_test_case_to_project(self.project, klass) assert_equal_strings(expected_output, module.get_content()) def test_keeps_future_imports_first(self): module = self._test_module_from_code("from __future__ import division\n") self.generator.ensure_import(('nose', 'SkipTest')) klass = self._test_class_from_code("", "TestClass", "test_method") add_test_case_to_project(self.project, klass) assert_matches(r"from __future__ import division.*from nose import SkipTest", module.get_content())
def test_removal_of_a_module_removes_its_code_tree(self): project = EmptyProject() mod = project.create_module('module.py') ct = CodeTree(None) project.remember_code_tree(ct, mod) project.remove_module(mod.subpath) assert_raises(CodeTreeNotFound, lambda: CodeTree.of(mod))
def test_adds_new_test_methods_to_existing_test_classes_inside_application_modules( self): project = EmptyProject().with_module("somethings.py") test_class = create(TestClass, name="TestSomething") add_test_case(project["somethings"], test_class) method = create(TestMethod) generated_test_class = create(TestClass, name="TestSomething", test_cases=[method]) add_test_case_to_project(project, generated_test_class) assert_length(get_test_cases(project), 1) assert_equal_sets([method], test_class.test_cases) assert method.parent is test_class
def setUp(self): self.generator = UnittestTestGenerator() self.project = EmptyProject() self.module = self.project.create_module("module.py") self.test_module = self.project.create_module("test_module.py")
def setUp(self): project = EmptyProject() self.module = Module(project=project, subpath='module.py') self.klass = Class('Klass', module=self.module) self.tclass = TestClass('TClass', parent=self.module)
class TestGeneratorAdderForProjectWithTestModule(CapturedLogger): def setUp(self): CapturedLogger.setUp(self) self.project = EmptyProject() self.existing_test_class = create(TestClass, name="TestSomething") self.test_module = self.project.create_module("test_module.py", code=EmptyCode()) add_test_case(self.test_module, self.existing_test_class) def _associate_module_with_existing_test_class(self): self.associated_module = self.project.create_module("module.py") self.existing_test_class.associated_modules = [self.associated_module] def test_attaches_test_class_to_test_module_with_most_test_cases_for_associated_module(self): self.project.create_module("irrelevant_test_module.py") self._associate_module_with_existing_test_class() new_test_class = create(TestClass, name="new", associated_modules=[self.associated_module]) add_test_case_to_project(self.project, new_test_class) assert new_test_class in self.test_module.test_cases def test_doesnt_overwrite_existing_test_classes_by_default(self): test_class = create(TestClass, name="TestSomething") add_test_case_to_project(self.project, test_class) assert_length(get_test_cases(self.project), 1) def test_adds_new_test_classes_to_existing_test_module(self): test_class = create(TestClass, name="TestSomethingNew", associated_modules=[self.project.create_module("module.py")]) add_test_case_to_project(self.project, test_class) assert_equal_sets([self.existing_test_class, test_class], get_test_cases(self.project)) def test_adds_new_test_methods_to_existing_test_classes(self): test_method = create(TestMethod, name="test_new_method") test_class = create(TestClass, name="TestSomething", test_cases=[test_method]) add_test_case_to_project(self.project, test_class) assert_length(get_test_cases(self.project), 1) assert get_test_cases(self.project)[0] is test_method.parent assert test_method.parent is not test_class # The right message was issued. assert_contains_once(self._get_log_output(), "Adding generated test_new_method to TestSomething in test_module.py.") def test_after_adding_new_test_case_to_class_its_module_is_marked_as_changed(self): add_test_case(self.existing_test_class, create(TestMethod, name="test_something_new")) assert self.test_module.changed def test_merges_imports_during_merging_of_test_classes(self): test_class = TestClass("TestSomething", imports=['new_import']) add_test_case_to_project(self.project, test_class) assert_equal(['new_import'], self.test_module.imports) def test_doesnt_overwrite_existing_test_methods_by_default(self): test_method = create(TestMethod, name="test_method") test_class = create(TestClass, name="TestSomething", test_cases=[test_method]) add_test_case_to_project(self.project, test_class) assert_equal([test_method], get_test_cases(self.project)[0].test_cases) # Let's try adding the same method again. new_test_method = create(TestMethod, name="test_method") new_test_class = create(TestClass, name="TestSomething", test_cases=[new_test_method]) add_test_case_to_project(self.project, new_test_class) assert_equal([test_method], get_test_cases(self.project)[0].test_cases) # The right message was issued. assert_contains_once(self._get_log_output(), "Test case TestSomething.test_method already exists in test_module.py, skipping.") def test_overwrites_existing_test_methods_with_force_option(self): test_method = create(TestMethod, name="test_method") test_class = create(TestClass, name="TestSomething", test_cases=[test_method]) add_test_case_to_project(self.project, test_class) assert_equal([test_method], get_test_cases(self.project)[0].test_cases) # Let's try adding the same method again with a force option # set to True. new_test_method = create(TestMethod, name="test_method") new_test_class = create(TestClass, name="TestSomething", test_cases=[new_test_method]) add_test_case_to_project(self.project, new_test_class, force=True) # The class is still the same. assert_equal([self.existing_test_class], get_test_cases(self.project)) # But the method got replaced. assert_equal([new_test_method], get_test_cases(self.project)[0].test_cases) # The right message was issued. assert_contains_once(self._get_log_output(), "Replacing TestSomething.test_method from test_module.py with generated version.") def test_should_not_touch_modules_with_errors(self): project = EmptyProject() module = project.create_module("module.py") test_module = project.create_module("test_module.py", errors=[Exception()]) add_test_case_to_project(project, create(TestClass, associated_modules=[module])) assert test_module.changed is False def test_should_emit_warning_when_trying_to_add_test_to_module_with_errors(self): project = EmptyProject() module = project.create_module("module.py") project.create_module("test_module.py", errors=[Exception()]) add_test_case_to_project(project, create(TestClass, name="FooTest", associated_modules=[module])) assert_contains_once(self._get_log_output(), "WARNING: Not adding FooTest to test_module.py, because " "of a failed inspection.")
def setUp(self): project = EmptyProject() self.code = object() # A unique fake object. self.module = Module(project=project, subpath='module.py') self.code_tree = CodeTree(self.code) project.remember_code_tree(self.code_tree, self.module)
class TestGeneratorWithSingleModule: def setUp(self): self.project = EmptyProject().with_modules(["module.py", "test_module.py"], create_files=False) self.project["module"].add_object(Function("function")) self.module_path = os.path.join(self.project.path, "module.py") def test_adds_imports_to_existing_test_files_only_if_they_arent_present(self): self.project["test_module"].imports = ['unittest'] add_tests_to_project(self.project, [self.module_path], 'unittest') assert_equal(['unittest'], self.project["test_module"].imports) self.project["test_module"].imports = [('nose', 'SkipTest')] add_tests_to_project(self.project, [self.module_path], 'unittest') assert_equal_sets(['unittest', ('nose', 'SkipTest')], self.project["test_module"].imports) def test_appends_new_test_classes_to_existing_test_files(self): TEST_CONTENTS = "class TestSomething: pass\n\n" module = inspect_code(self.project, "test_module.py", TEST_CONTENTS) add_tests_to_project(self.project, [self.module_path], 'unittest') assert_contains(module.get_content(), TEST_CONTENTS) assert_contains(module.get_content(), "class TestFunction(unittest.TestCase):") def test_associates_test_cases_with_application_modules(self): add_tests_to_project(self.project, [self.module_path], 'unittest') project_test_cases = get_test_cases(self.project) assert_length(project_test_cases, 1) assert_equal(project_test_cases[0].associated_modules, [self.project["module"]]) def test_chooses_the_right_existing_test_module_for_new_test_case(self): self.project.create_module("test_other.py") add_tests_to_project(self.project, [self.module_path], 'unittest') assert_length(self.project["test_module"].test_cases, 1) assert_length(self.project["test_other"].test_cases, 0) def test_comments_assertions_with_user_objects_that_cannot_be_constructed(self): klass, instance = ClassWithInstanceWithoutReconstruction("Something") function = FunctionWithSingleCall("nofun", {'x': instance}, "something else") self.project["module"].add_objects([klass, function]) add_tests_to_project(self.project, [self.module_path], 'unittest') result = self.project["test_module"].get_content() assert_contains(result, "def test_nofun_returns_something_else_for_something_instance(self):") assert_contains(result, "# self.assertEqual('something else', nofun(<TODO: test.test_generator.Something>))") def test_generates_type_assertions_for_calls_with_composite_objects_which_elements_cannot_be_constructed(self): klass, instance = ClassWithInstanceWithoutReconstruction("Unspeakable") function = FunctionWithSingleCall("morefun", {}, [instance]) self.project["module"].add_objects([klass, function]) add_tests_to_project(self.project, [self.module_path], 'unittest') result = self.project["test_module"].get_content() assert_contains(result, "def test_morefun_returns_list(self):") assert_contains(result, "self.assertEqual(list, type(morefun()))")
def _inspect_code(self, code): return inspect_code(EmptyProject(), "module.py", code)
def test_raises_module_not_found_exception_when_no_module_like_that_is_present( self): project = EmptyProject() assert_raises(ModuleNotFound, lambda: project["whatever"])
def setUp(self): self.project = EmptyProject().with_modules(["module.py", "test_module.py"], create_files=False) self.project["module"].add_object(Function("function")) self.module_path = os.path.join(self.project.path, "module.py")
class TestGeneratorAdderForProjectWithTestModule(CapturedLogger): def setUp(self): CapturedLogger.setUp(self) self.project = EmptyProject() self.existing_test_class = create(TestClass, name="TestSomething") self.test_module = self.project.create_module("test_module.py", code=EmptyCode()) add_test_case(self.test_module, self.existing_test_class) def _associate_module_with_existing_test_class(self): self.associated_module = self.project.create_module("module.py") self.existing_test_class.associated_modules = [self.associated_module] def test_attaches_test_class_to_test_module_with_most_test_cases_for_associated_module( self): self.project.create_module("irrelevant_test_module.py") self._associate_module_with_existing_test_class() new_test_class = create(TestClass, name="new", associated_modules=[self.associated_module]) add_test_case_to_project(self.project, new_test_class) assert new_test_class in self.test_module.test_cases def test_doesnt_overwrite_existing_test_classes_by_default(self): test_class = create(TestClass, name="TestSomething") add_test_case_to_project(self.project, test_class) assert_length(get_test_cases(self.project), 1) def test_adds_new_test_classes_to_existing_test_module(self): test_class = create( TestClass, name="TestSomethingNew", associated_modules=[self.project.create_module("module.py")]) add_test_case_to_project(self.project, test_class) assert_equal_sets([self.existing_test_class, test_class], get_test_cases(self.project)) def test_adds_new_test_methods_to_existing_test_classes(self): test_method = create(TestMethod, name="test_new_method") test_class = create(TestClass, name="TestSomething", test_cases=[test_method]) add_test_case_to_project(self.project, test_class) assert_length(get_test_cases(self.project), 1) assert get_test_cases(self.project)[0] is test_method.parent assert test_method.parent is not test_class # The right message was issued. assert_contains_once( self._get_log_output(), "Adding generated test_new_method to TestSomething in test_module.py." ) def test_after_adding_new_test_case_to_class_its_module_is_marked_as_changed( self): add_test_case(self.existing_test_class, create(TestMethod, name="test_something_new")) assert self.test_module.changed def test_merges_imports_during_merging_of_test_classes(self): test_class = TestClass("TestSomething", imports=['new_import']) add_test_case_to_project(self.project, test_class) assert_equal(['new_import'], self.test_module.imports) def test_doesnt_overwrite_existing_test_methods_by_default(self): test_method = create(TestMethod, name="test_method") test_class = create(TestClass, name="TestSomething", test_cases=[test_method]) add_test_case_to_project(self.project, test_class) assert_equal([test_method], get_test_cases(self.project)[0].test_cases) # Let's try adding the same method again. new_test_method = create(TestMethod, name="test_method") new_test_class = create(TestClass, name="TestSomething", test_cases=[new_test_method]) add_test_case_to_project(self.project, new_test_class) assert_equal([test_method], get_test_cases(self.project)[0].test_cases) # The right message was issued. assert_contains_once( self._get_log_output(), "Test case TestSomething.test_method already exists in test_module.py, skipping." ) def test_overwrites_existing_test_methods_with_force_option(self): test_method = create(TestMethod, name="test_method") test_class = create(TestClass, name="TestSomething", test_cases=[test_method]) add_test_case_to_project(self.project, test_class) assert_equal([test_method], get_test_cases(self.project)[0].test_cases) # Let's try adding the same method again with a force option # set to True. new_test_method = create(TestMethod, name="test_method") new_test_class = create(TestClass, name="TestSomething", test_cases=[new_test_method]) add_test_case_to_project(self.project, new_test_class, force=True) # The class is still the same. assert_equal([self.existing_test_class], get_test_cases(self.project)) # But the method got replaced. assert_equal([new_test_method], get_test_cases(self.project)[0].test_cases) # The right message was issued. assert_contains_once( self._get_log_output(), "Replacing TestSomething.test_method from test_module.py with generated version." ) def test_should_not_touch_modules_with_errors(self): project = EmptyProject() module = project.create_module("module.py") test_module = project.create_module("test_module.py", errors=[Exception()]) add_test_case_to_project( project, create(TestClass, associated_modules=[module])) assert test_module.changed is False def test_should_emit_warning_when_trying_to_add_test_to_module_with_errors( self): project = EmptyProject() module = project.create_module("module.py") project.create_module("test_module.py", errors=[Exception()]) add_test_case_to_project( project, create(TestClass, name="FooTest", associated_modules=[module])) assert_contains_once( self._get_log_output(), "WARNING: Not adding FooTest to test_module.py, because " "of a failed inspection.")
def test_uses_system_specific_path_separator(self): module = Module(subpath="some#path.py", project=EmptyProject()) assert_equal("some.path", module.locator)
def setUp(self): self.project = EmptyProject() self.module = self.project.create_module("module.py", code=parse("# only comments")) self.test_class = TestClass(name="TestSomething", code=parse("# some test code"))