def test_no_exceptions(short_test_case): config.INSTANCE.module_name = "tests.fixtures.accessibles.accessible" tracer = ExecutionTracer() with install_import_hook(config.INSTANCE.module_name, tracer): executor = TestCaseExecutor(tracer) result = executor.execute([short_test_case]) assert not result.has_test_exceptions()
def test_compute_fitness_values_no_branches(): module_name = "tests.fixtures.branchcoverage.nobranches" tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident with install_import_hook(module_name, tracer): module = importlib.import_module(module_name) importlib.reload(module) executor = TestCaseExecutor(tracer) chromosome = _get_test_for_no_branches_fixture(module) goals = bcf.BranchCoverageFactory(executor).get_coverage_goals() goals_dict = {} for goal in goals: chromosome.add_fitness_function(goal) goals_dict[tracer.get_known_data().existing_code_objects[ goal._goal.code_object_id].code_object.co_name] = goal fitness = chromosome.get_fitness() assert fitness == 1 assert chromosome.fitness_values[goals_dict["__init__"]].fitness == 0.0 assert chromosome.fitness_values[goals_dict["other"]].fitness == 1.0 assert chromosome.fitness_values[goals_dict["<module>"]].fitness == 0.0 assert chromosome.fitness_values[goals_dict["get_x"]].fitness == 0.0 assert chromosome.fitness_values[goals_dict["identity"]].fitness == 0.0 assert chromosome.fitness_values[ goals_dict["DummyClass"]].fitness == 0.0
def _setup_path_and_hook(self) -> Optional[ExecutionTracer]: """Inserts the path to the SUT into the path list. Also installs the import hook.""" if not os.path.isdir(config.INSTANCE.project_path): self._logger.error( "%s is not a valid project path", config.INSTANCE.project_path ) return None self._logger.debug("Setting up path for %s", config.INSTANCE.project_path) sys.path.insert(0, config.INSTANCE.project_path) self._logger.debug( "Setting up instrumentation for %s", config.INSTANCE.module_name ) tracer = ExecutionTracer() install_import_hook(config.INSTANCE.module_name, tracer) return tracer
def test_hook(): tracer = ExecutionTracer() with install_import_hook("tests.fixtures.instrumentation.mixed", tracer): module = importlib.import_module( "tests.fixtures.instrumentation.mixed") importlib.reload(module) assert len(tracer.get_known_data().existing_code_objects) > 0 assert module.function(6) == 0
def test_simple_execution(): config.INSTANCE.module_name = "tests.fixtures.accessibles.accessible" tracer = ExecutionTracer() with install_import_hook(config.INSTANCE.module_name, tracer): test_case = dtc.DefaultTestCase() test_case.add_statement(prim_stmt.IntPrimitiveStatement(test_case, 5)) executor = TestCaseExecutor(tracer) assert not executor.execute([test_case]).has_test_exceptions()
def test_no_exceptions(short_test_case): config.configuration.module_name = "tests.fixtures.accessibles.accessible" tracer = ExecutionTracer() with install_import_hook(config.configuration.module_name, tracer): module = importlib.import_module(config.configuration.module_name) importlib.reload(module) executor = TestCaseExecutor(tracer) result = executor.execute(short_test_case) assert not result.has_test_exceptions()
def test_simple_execution(): config.configuration.module_name = "tests.fixtures.accessibles.accessible" tracer = ExecutionTracer() with install_import_hook(config.configuration.module_name, tracer): module = importlib.import_module(config.configuration.module_name) importlib.reload(module) test_case = dtc.DefaultTestCase() test_case.add_statement(prim_stmt.IntPrimitiveStatement(test_case, 5)) executor = TestCaseExecutor(tracer) assert not executor.execute(test_case).has_test_exceptions()
def test_illegal_call(method_mock): config.INSTANCE.module_name = "tests.fixtures.accessibles.accessible" test_case = dtc.DefaultTestCase() int_stmt = prim_stmt.IntPrimitiveStatement(test_case, 5) method_stmt = param_stmt.MethodStatement(test_case, method_mock, int_stmt.return_value) test_case.add_statement(int_stmt) test_case.add_statement(method_stmt) tracer = ExecutionTracer() with install_import_hook(config.INSTANCE.module_name, tracer): executor = TestCaseExecutor(tracer) result = executor.execute([test_case]) assert result.has_test_exceptions()
def test_compute_fitness_values_nested_branches(): module_name = "tests.fixtures.branchcoverage.nestedbranches" tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident with install_import_hook(module_name, tracer): module = importlib.import_module(module_name) importlib.reload(module) executor = TestCaseExecutor(tracer) chromosome = _get_test_for_nested_branch_fixture(module) goals = bcf.BranchCoverageFactory(executor).get_coverage_goals() for goal in goals: chromosome.add_fitness_function(goal) fitness = chromosome.get_fitness() assert fitness == pytest.approx(5.90782493)
def test_illegal_call(method_mock): config.configuration.module_name = "tests.fixtures.accessibles.accessible" test_case = dtc.DefaultTestCase() int_stmt = prim_stmt.IntPrimitiveStatement(test_case, 5) method_stmt = param_stmt.MethodStatement(test_case, method_mock, int_stmt.ret_val) test_case.add_statement(int_stmt) test_case.add_statement(method_stmt) tracer = ExecutionTracer() with install_import_hook(config.configuration.module_name, tracer): module = importlib.import_module(config.configuration.module_name) importlib.reload(module) executor = TestCaseExecutor(tracer) result = executor.execute(test_case) assert result.has_test_exceptions()
def test_module_instrumentation_integration(): """Small integration test, which tests the instrumentation for various function types.""" tracer = ExecutionTracer() with install_import_hook("tests.fixtures.instrumentation.mixed", tracer): mixed = importlib.import_module("tests.fixtures.instrumentation.mixed") mixed = importlib.reload(mixed) inst = mixed.TestClass(5) inst.method(5) inst.method_with_nested(5) mixed.function(5) sum(mixed.generator()) asyncio.run(mixed.coroutine(5)) asyncio.run(run_async_generator(mixed.async_generator())) assert len(tracer.get_trace().executed_code_objects) == 10
def test_integrate_randoopy(module_name: str): config.configuration.budget = 1 config.configuration.algorithm = config.Algorithm.RANDOM config.configuration.module_name = module_name logger = MagicMock(Logger) tracer = ExecutionTracer() with install_import_hook(module_name, tracer): # Need to force reload in order to apply instrumentation. module = importlib.import_module(module_name) importlib.reload(module) executor = TestCaseExecutor(tracer) cluster = TestClusterGenerator(module_name).generate_cluster() algorithm = gaf.TestSuiteGenerationAlgorithmFactory( executor, cluster).get_search_algorithm() algorithm._logger = logger test_cases = algorithm.generate_tests() assert test_cases.size() >= 0
def test_integrate_randoopy(algorithm_to_run: Callable, module_name: str): config.INSTANCE.budget = 1 config.INSTANCE.module_name = module_name logger = MagicMock(Logger) tracer = ExecutionTracer() with install_import_hook(module_name, tracer): # Need to force reload in order to apply instrumentation. module = importlib.import_module(module_name) importlib.reload(module) executor = TestCaseExecutor(tracer) algorithm = algorithm_to_run( executor, TestClusterGenerator(module_name).generate_cluster()) algorithm._logger = logger test_cases, failing_test_cases = algorithm.generate_sequences() assert test_cases.size() >= 0 assert failing_test_cases.size() >= 0
def test_integrate_wspy(module_name: str): # TODO(fk) reduce direct dependencies to config.INSTANCE config.INSTANCE.budget = 1 config.INSTANCE.module_name = module_name config.INSTANCE.population = 3 config.INSTANCE.min_initial_tests = 1 config.INSTANCE.max_initial_tests = 1 logger = MagicMock(Logger) tracer = ExecutionTracer() with install_import_hook(module_name, tracer): # Need to force reload in order to apply instrumentation. module = importlib.import_module(module_name) importlib.reload(module) executor = TestCaseExecutor(tracer) algorithm = WholeSuiteTestStrategy( executor, TestClusterGenerator(module_name).generate_cluster() ) algorithm._logger = logger test_cases, failing_test_cases = algorithm.generate_sequences() assert test_cases.size() >= 0 assert failing_test_cases.size() >= 0
def test_integrate_randomsearch(module_name: str): # TODO(fk) reduce direct dependencies to config.INSTANCE config.configuration.algorithm = config.Algorithm.RANDOM_SEARCH config.configuration.budget = 1 config.configuration.module_name = module_name config.configuration.min_initial_tests = 1 config.configuration.max_initial_tests = 1 logger = MagicMock(Logger) tracer = ExecutionTracer() with install_import_hook(module_name, tracer): # Need to force reload in order to apply instrumentation. module = importlib.import_module(module_name) importlib.reload(module) executor = TestCaseExecutor(tracer) cluster = TestClusterGenerator(module_name).generate_cluster() algorithm = gaf.TestSuiteGenerationAlgorithmFactory( executor, cluster ).get_search_algorithm() algorithm._logger = logger test_cases = algorithm.generate_tests() assert test_cases.size() >= 0
def _setup_import_hook() -> ExecutionTracer: _LOGGER.debug("Setting up instrumentation for %s", config.configuration.module_name) tracer = ExecutionTracer() install_import_hook(config.configuration.module_name, tracer) return tracer
def _setup_import_hook(self) -> ExecutionTracer: self._logger.debug("Setting up instrumentation for %s", config.INSTANCE.module_name) tracer = ExecutionTracer() install_import_hook(config.INSTANCE.module_name, tracer) return tracer