def _test_appending(self, modified_input, expected_output): project = ProjectInDirectory(self.tmpdir) module_path = putfile(project.path, "module.py", read_data("appending_test_cases_module_initial.py")) test_module_path = putfile(project.path, "test_module.py", read_data("appending_test_cases_output_initial.py")) # Analyze the project with an existing test module. inspect_project(project) # Filesystem stat has resolution of 1 second, and we don't want to # sleep in a test, so we just fake the original files creation time. project["module"].created = 0 project["test_module"].created = 0 # Modify the application module and analyze it again. putfile(project.path, "module.py", read_data(modified_input)) inspect_project(project) # Regenerate the tests. add_tests_to_project(project, [module_path], 'unittest') project.save() assert_length(project.get_modules(), 2) result = read_file_contents(test_module_path) expected_result = read_data(expected_output) assert_equal_strings(expected_result, result)
def test_doesnt_generate_test_files_with_no_test_cases(self): project = ProjectInDirectory(self.tmpdir).with_modules(["module.py"]) test_file = os.path.join(project.path, "test_module.py") add_tests_to_project(project, [os.path.join(project.path, "module.py")], 'unittest') assert not os.path.exists(test_file)
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 generate_single_test_module(template='unittest', **module_kwds): """Return test module contents generated for given module. """ project = EmptyProject() project.create_module("module.py", code=EmptyCode(), **module_kwds) add_tests_to_project(project, ["module.py"], template, False) return get_test_module_contents(project)
def _test_appending(self, modified_input, expected_output): project = ProjectInDirectory(self.tmpdir) module_path = putfile( project.path, "module.py", read_data("appending_test_cases_module_initial.py")) test_module_path = putfile( project.path, "test_module.py", read_data("appending_test_cases_output_initial.py")) # Analyze the project with an existing test module. inspect_project(project) # Filesystem stat has resolution of 1 second, and we don't want to # sleep in a test, so we just fake the original files creation time. project["module"].created = 0 project["test_module"].created = 0 # Modify the application module and analyze it again. putfile(project.path, "module.py", read_data(modified_input)) inspect_project(project) # Regenerate the tests. add_tests_to_project(project, [module_path], 'unittest') project.save() assert_length(project.get_modules(), 2) result = read_file_contents(test_module_path) expected_result = read_data(expected_output) assert_equal_strings(expected_result, result)
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_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_creates_new_test_module_if_no_of_the_existing_match(self): project = TestableProject(self.tmpdir, ["test_other.py"]) add_tests_to_project(project, [os.path.join(project.path, "module.py")], 'unittest') project_test_cases = get_test_cases(project) assert_length(project_test_cases, 1) assert_length(project["test_other"].test_cases, 0)
def test_debug_output_includes_packages_and_module_names(self): project = EmptyProject().with_module("module.py", [Function('some_function')]) add_tests_to_project(project, ["module"], 'unittest') assert_matches(r"\d+\.\d+ generator:\d+ INFO: Generating tests for module module.py.\n", self._get_log_output(), anywhere=True) assert_matches(r"\d+\.\d+ generator\.adder:\d+ INFO: Adding generated TestSomeFunction to %s.\n" % re.escape(P("tests/test_module.py")), self._get_log_output(), anywhere=True)
def test_reports_each_module_it_generates_tests_for(self): paths = ["first.py", "another.py", P("one/more.py")] project = EmptyProject().with_modules(paths, create_files=False) add_tests_to_project(project, paths, 'unittest') for path in paths: assert_contains_once(self._get_log_output(), "Generating tests for module %s." % path)
def test_generates_test_stubs(self): expected_result = read_data("static_analysis_output.py") project = ProjectInDirectory(self.tmpdir) module_path = putfile(project.path, "module.py", read_data("static_analysis_module.py")) inspect_project(project) add_tests_to_project(project, [module_path], 'unittest') result = get_test_module_contents(project) assert_equal_strings(expected_result, result)
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 execute_with_point_of_entry_and_assert(self, id): expected_result = read_data("%s_output.py" % id) project = ProjectInDirectory(self.tmpdir).with_points_of_entry(["poe.py"]) module_path = putfile(project.path, "module.py", read_data("%s_module.py" % id)) write_content_to_file(read_data("generic_acceptance_poe.py"), project.path_for_point_of_entry("poe.py")) inspect_project(project) add_tests_to_project(project, [module_path], 'unittest') result = get_test_module_contents(project) assert_equal_strings(expected_result, result)
def test_uses_existing_destination_directory(self): project = ProjectInDirectory(self.tmpdir) add_tests_to_project(project, [], 'unittest')
def add_and_save(): add_tests_to_project(project, [os.path.join(project.path, "module.py")], 'unittest') project.save()
def add_and_save(): add_tests_to_project(self.project, [self.module_path], 'unittest') self.project.save()
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"]])