Ejemplo n.º 1
0
def test_compute_fitness_values(known_data_mock, executor_mock, trace_mock):
    tracer = MagicMock()
    tracer.get_known_data.return_value = known_data_mock
    executor_mock.tracer.return_value = tracer
    ff = BranchDistanceTestCaseFitnessFunction(executor_mock)
    indiv = MagicMock()
    with mock.patch.object(ff, "_run_test_case_chromosome") as run_suite_mock:
        result = ExecutionResult()
        result.execution_trace = trace_mock
        run_suite_mock.return_value = result
        assert ff.compute_fitness_values(indiv) == FitnessValues(0, 1)
        run_suite_mock.assert_called_with(indiv)
Ejemplo n.º 2
0
 def _after_test_case_execution(
     self, test_case: tc.TestCase, result: res.ExecutionResult
 ) -> None:
     """Collect the execution trace after each executed test case."""
     result.execution_trace = self._tracer.get_trace()
     for observer in self._observers:
         observer.after_test_case_execution(test_case, result)
 def _collect_execution_trace(self, result: res.ExecutionResult):
     """
     Collect the fitness after each execution.
     Also clear the tracking results so far.
     """
     result.execution_trace = self._tracer.get_trace()
     self._tracer.clear_trace()
Ejemplo n.º 4
0
def test_compute_fitness_values_mocked(known_data_mock, executor_mock,
                                       trace_mock):
    tracer = MagicMock()
    tracer.get_known_data.return_value = known_data_mock
    executor_mock.tracer.return_value = tracer
    goal = MagicMock(bcg.AbstractBranchCoverageGoal)
    goal.get_distance.return_value = cfd.ControlFlowDistance(1, 2)
    ff = bctf.BranchCoverageTestFitness(executor_mock, goal)
    indiv = MagicMock()
    with mock.patch.object(ff, "_run_test_case_chromosome") as run_suite_mock:
        result = ExecutionResult()
        result.execution_trace = trace_mock
        run_suite_mock.return_value = result
        fitness_values = ff.compute_fitness_values(indiv)
        assert fitness_values.coverage == 0
        assert pytest.approx(1.666666, fitness_values.fitness)
        run_suite_mock.assert_called_with(indiv)
Ejemplo n.º 5
0
 def _execute_nodes(
     self, exec_ctx: ctx.ExecutionContext, result: res.ExecutionResult,
 ):
     for idx, node in enumerate(exec_ctx.executable_nodes()):
         try:
             if self._logger.isEnabledFor(logging.DEBUG):
                 self._logger.debug("Executing %s", astor.to_source(node))
             code = compile(node, "<ast>", "exec")
             # pylint: disable=exec-used
             exec(code, exec_ctx.global_namespace, exec_ctx.local_namespace)  # nosec
         except Exception as err:  # pylint: disable=broad-except
             failed_stmt = astor.to_source(node)
             TestCaseExecutor._logger.debug(
                 "Failed to execute statement:\n%s%s", failed_stmt, err.args
             )
             result.report_new_thrown_exception(idx, err)
             break
Ejemplo n.º 6
0
 def after_test_case_execution(self, test_case: tc.TestCase,
                               result: res.ExecutionResult):
     result.add_output_trace(type(self), self.get_trace())
Ejemplo n.º 7
0
def execution_result():
    return ExecutionResult(timeout=True)
Ejemplo n.º 8
0
def test_default_values():
    result = ExecutionResult()
    assert not result.has_test_exceptions()
Ejemplo n.º 9
0
def test_get_first_position_of_ex_none():
    result = ExecutionResult()
    assert result.get_first_position_of_thrown_exception() is None
Ejemplo n.º 10
0
def test_get_first_position_of_ex():
    result = ExecutionResult()
    result.report_new_thrown_exception(5, Exception())
    result.report_new_thrown_exception(3, Exception())
    assert result.get_first_position_of_thrown_exception() == 3
Ejemplo n.º 11
0
def test_fitness_setter():
    result = ExecutionResult()
    result.fitness = 5.0
    assert result.fitness == 5.0
Ejemplo n.º 12
0
def test_exceptions():
    result = ExecutionResult()
    ex = Exception()
    result.report_new_thrown_exception(0, ex)
    assert result.exceptions[0] == ex
Ejemplo n.º 13
0
def test_report_new_thrown_exception():
    result = ExecutionResult()
    result.report_new_thrown_exception(0, Exception())
    assert result.has_test_exceptions()