def test_bool_distance_false(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_predicate(MagicMock(code_object_id=0)) tracer.executed_bool_predicate(False, 0) assert (0, 1.0) in tracer.get_trace().true_distances.items() assert (0, 0.0) in tracer.get_trace().false_distances.items()
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 test_update_metrics_covered(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_predicate(MagicMock(code_object_id=0)) tracer.executed_compare_predicate(1, 0, 0, Compare.EQ) tracer.executed_compare_predicate(1, 0, 0, Compare.EQ) assert (0, 2) in tracer.get_trace().executed_predicates.items()
def test_cmp(cmp, val1, val2, true_dist, false_dist): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_predicate(MagicMock(code_object_id=0)) tracer.executed_compare_predicate(val1, val2, 0, cmp) assert (0, true_dist) in tracer.get_trace().true_distances.items() assert (0, false_dist) in tracer.get_trace().false_distances.items()
def test_code_object_executed_other_thread(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_code_object(MagicMock()) thread = threading.Thread(target=tracer.executed_code_object, args=(0,)) thread.start() thread.join() assert tracer.get_trace().executed_code_objects == set()
def test_update_metrics_true_dist_min(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_predicate(MagicMock(code_object_id=0)) tracer.executed_compare_predicate(5, 0, 0, Compare.EQ) assert (0, 5) in tracer.get_trace().true_distances.items() tracer.executed_compare_predicate(4, 0, 0, Compare.EQ) assert (0, 4) in tracer.get_trace().true_distances.items()
def test_update_metrics_false_dist_min(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_predicate(MagicMock(code_object_id=0)) tracer.executed_compare_predicate(3, 1, 0, Compare.NE) assert (0, 2) in tracer.get_trace().false_distances.items() tracer.executed_compare_predicate(2, 1, 0, Compare.NE) assert (0, 1) in tracer.get_trace().false_distances.items()
def test_clear(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_code_object(MagicMock(CodeObjectMetaData)) tracer.executed_code_object(0) trace = tracer.get_trace() tracer.clear_trace() assert tracer.get_trace() != trace
def test_bool_predicate_executed_other_thread(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_code_object(MagicMock()) tracer.register_code_object(MagicMock(code_object_id=0)) thread = threading.Thread(target=tracer.executed_bool_predicate, args=(True, 0)) thread.start() thread.join() assert tracer.get_trace().executed_predicates == {}
def _load_sut(tracer: ExecutionTracer) -> bool: try: # We need to set the current thread ident so the import trace is recorded. tracer.current_thread_ident = threading.currentThread().ident importlib.import_module(config.configuration.module_name) except ImportError as ex: # A module could not be imported because some dependencies # are missing or it is malformed _LOGGER.exception("Failed to load SUT: %s", ex) return False return True
def test_enable_disable_cmp(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_predicate(MagicMock(code_object_id=0)) assert len(tracer.get_trace().executed_predicates) == 0 tracer.disable() tracer.executed_compare_predicate(0, 0, 0, Compare.EQ) assert len(tracer.get_trace().executed_predicates) == 0 tracer.enable() tracer.executed_compare_predicate(0, 0, 0, Compare.EQ) assert len(tracer.get_trace().executed_predicates) == 1
def test_enable_disable_bool(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_predicate(MagicMock(code_object_id=0)) assert len(tracer.get_trace().executed_predicates) == 0 tracer.disable() tracer.executed_bool_predicate(True, 0) assert len(tracer.get_trace().executed_predicates) == 0 tracer.enable() tracer.executed_bool_predicate(True, 0) assert len(tracer.get_trace().executed_predicates) == 1
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_module_instrumentation_integration(): """Small integration test, which tests the instrumentation for various function types.""" tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident 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_passed_bool_predicate(): tracer = ExecutionTracer() tracer.register_predicate(MagicMock(code_object_id=0)) tracer.current_thread_ident = threading.currentThread().ident tracer.executed_bool_predicate(True, 0) assert (0, 1) in tracer.get_trace().executed_predicates.items()
def test_unknown_comp(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_predicate(MagicMock(code_object_id=0)) with pytest.raises(Exception): tracer.executed_compare_predicate(1, 1, 0, Compare.EXC_MATCH)
def test_entered_function(): tracer = ExecutionTracer() tracer.current_thread_ident = threading.currentThread().ident tracer.register_code_object(MagicMock(CodeObjectMetaData)) tracer.executed_code_object(0) assert 0 in tracer.get_trace().executed_code_objects