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 _get_fitness_functions(self) -> List[ff.FitnessFunction]: """Converts a criterion into a test suite fitness function. Returns: A list of fitness functions """ if config.configuration.algorithm == config.Algorithm.MOSA: factory = bcf.BranchCoverageFactory(self._executor) fitness_functions: List[ff.FitnessFunction] = factory.get_coverage_goals() self._logger.info( "Instantiated %d fitness functions", len(fitness_functions) ) return fitness_functions return [self._get_test_suite_fitness_function()]
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)