Beispiel #1
0
def test_timer_as_context_manager(capsys):
    with Timer(text=TIME_MESSAGE):
        waste_time()
    stdout, stderr = capsys.readouterr()
    assert RE_TIME_MESSAGE.match(stdout)
    assert stdout.count("\n") == 1
    assert stderr == ""
Beispiel #2
0
def _setup_test_cluster() -> Optional[TestCluster]:
    with Timer(name="Test-cluster generation time", logger=None):
        test_cluster = TestClusterGenerator(
            config.configuration.module_name).generate_cluster()
        if test_cluster.num_accessible_objects_under_test() == 0:
            _LOGGER.error("SUT contains nothing we can test.")
            return None
        return test_cluster
    def generate_sequence(
        self,
        test_chromosome: tsc.TestSuiteChromosome,
        failing_test_chromosome: tsc.TestSuiteChromosome,
        execution_counter: int,
    ) -> None:
        """Implements one step of the adapted Randoop algorithm.

        Args:
            test_chromosome: The list of currently successful test cases
            failing_test_chromosome: The list of currently not successful test cases
            execution_counter: A current number of algorithm iterations

        Raises:
            GenerationException: In case an error occurs during generation
        """
        self._logger.info("Algorithm iteration %d", execution_counter)
        timer = Timer(name="Sequence generation", logger=None)
        timer.start()
        objects_under_test: Set[
            gao.
            GenericAccessibleObject] = self.test_cluster.accessible_objects_under_test

        if not objects_under_test:
            # In case we do not have any objects under test, we cannot generate a
            # test case.
            raise GenerationException(
                "Cannot generate test case without an object-under-test!")

        # Create new test case, i.e., sequence in Randoop paper terminology
        # Pick a random public method from objects under test
        method = self._random_public_method(objects_under_test)
        # Select random test cases from existing ones to base generation on
        tests = self._random_test_cases(test_chromosome.test_chromosomes)
        new_test: tc.TestCase = dtc.DefaultTestCase()
        for test in tests:
            new_test.append_test_case(test)

        # Generate random values as input for the previously picked random method
        # Extend the test case by the new method call
        self.test_factory.append_generic_statement(new_test, method)

        # Discard duplicates
        if (new_test in test_chromosome.test_chromosomes
                or new_test in failing_test_chromosome.test_chromosomes):
            return

        with Timer(name="Execution time", logger=None):
            # Execute new sequence
            exec_result = self._executor.execute([new_test])

        # Classify new test case and outputs
        if exec_result.has_test_exceptions():
            failing_test_chromosome.add_test(new_test)
        else:
            test_chromosome.add_test(new_test)
            # TODO(sl) What about extensible flags?
        self._execution_results.append(exec_result)
        timer.stop()
Beispiel #4
0
def test_running_cleared_timers():
    t = Timer(name="timer_to_be_cleared")
    Timer.timers.clear()

    accumulated_time_waste()
    with t:
        waste_time()

    assert "accumulator" in Timer.timers
    assert "timer_to_be_cleared" in Timer.timers
Beispiel #5
0
def text_accumulated_contextmanager(capsys):
    t = Timer(name="accumulator", text=TIME_MESSAGE)
    with t:
        waste_time()
    with t:
        waste_time()
    stdout, stderr = capsys.readouterr()
    lines = stdout.strip().split("\n")
    assert len(lines) == 2
    assert RE_TIME_MESSAGE.match(lines[0])
    assert RE_TIME_MESSAGE.match(lines[1])
    assert stderr == ""
Beispiel #6
0
def test_timers_stats():
    name = "timer_with_stats"
    t = Timer(name=name)
    for num in range(5, 10):
        with t:
            waste_time(num=100 * num)

    stats = Timer.timers
    assert stats.total(name) == stats[name]
    assert stats.count(name) == 5
    assert stats.min(name) <= stats.median(name) <= stats.max(name)
    assert stats.mean(name) >= stats.min(name)
    assert stats.std_dev(name) >= 0
Beispiel #7
0
def test_accumulated_explicit_timer(capsys):
    t = Timer(name="accumulated_explicit_timer", text=TIME_MESSAGE)
    total = 0
    t.start()
    waste_time()
    total += t.stop()
    t.start()
    waste_time()
    total += t.stop()
    stdout, stderr = capsys.readouterr()
    lines = stdout.strip().split("\n")
    assert len(lines) == 2
    assert RE_TIME_MESSAGE.match(lines[0])
    assert RE_TIME_MESSAGE.match(lines[1])
    assert stderr == ""
    assert total == Timer.timers["accumulated_explicit_timer"]
Beispiel #8
0
def test_explicit_timer(capsys):
    t = Timer(text=TIME_MESSAGE)
    t.start()
    waste_time()
    t.stop()
    stdout, stderr = capsys.readouterr()
    assert RE_TIME_MESSAGE.match(stdout)
    assert stdout.count("\n") == 1
    assert stderr == ""
    def execute_test_case_monkey_type(self, test_cases: List[tc.TestCase],
                                      test_cluster: TestCluster) -> None:
        """Handles a list of test cases, i.e., executes them and propagates the results back.

        The test cases will be executed while MonkeyType is tracking all calls.
        Afterwards, the results, i.e., the tracked types for calls, are collected
        from the execution and the present type information gets updated accordingly.
        See the documentation of the `MonkeyTypeExecutor` for details.

        Currently, the update does only a simple `Union` of the existing and the
        newly inferred types.  See the documentation of `typing.Union` for details on
        how these `Union`s are handled.

        :param test_cases: The test cases to execute
        :param test_cluster: The underlying test cluster
        """
        with Timer(name="MonkeyType execution", logger=None):
            results = self._monkey_type_executor.execute(test_cases)
            self._monkey_type_executions += 1
            for result in results:
                self._update_type_inference(result, test_cluster)
Beispiel #10
0
    stat.track_output_variable(
        RuntimeVariable.AccessibleObjectsUnderTest,
        test_cluster.num_accessible_objects_under_test(),
    )
    stat.track_output_variable(
        RuntimeVariable.GeneratableTypes,
        len(test_cluster.get_all_generatable_types()),
    )


def _run() -> ReturnCode:
    if (setup_result := _setup_and_check()) is None:
        return ReturnCode.SETUP_FAILED
    executor, test_cluster = setup_result

    with Timer(name="Test generation time", logger=None):
        algorithm: TestGenerationStrategy = _instantiate_test_generation_strategy(
            executor, test_cluster)
        _LOGGER.info("Start generating sequences using %s",
                     config.configuration.algorithm)
        stat.set_sequence_start_time(time.time_ns())
        generation_result = algorithm.generate_tests()
        _LOGGER.info("Stop generating sequences using %s",
                     config.configuration.algorithm)
        algorithm.send_statistics()

        with Timer(name="Re-execution time", logger=None):
            stat.track_output_variable(RuntimeVariable.Coverage,
                                       generation_result.get_coverage())

        if config.configuration.post_process:
Beispiel #11
0
def test_timer_without_text(capsys):
    with Timer(logger=None):
        waste_time()
    stdout, stderr = capsys.readouterr()
    assert stdout == ""
    assert stderr == ""
Beispiel #12
0
def test_custom_logger():
    logger = CustomLogger()
    with Timer(text=TIME_MESSAGE, logger=logger):
        waste_time()
    assert RE_TIME_MESSAGE.match(logger.messages)
Beispiel #13
0
def test_access_timer_object_in_context(capsys):
    with Timer(text=TIME_MESSAGE) as t:
        assert isinstance(t, Timer)
        assert t.text.startswith(TIME_PREFIX)
    _, _ = capsys.readouterr()  # Do not print log message to standard out
Beispiel #14
0
def test_error_if_timer_not_running():
    t = Timer(text=TIME_MESSAGE)
    with pytest.raises(TimerError):
        t.stop()
Beispiel #15
0
def test_timers_cleared():
    with Timer(name="timer_to_be_cleared"):
        waste_time()
    assert "timer_to_be_cleared" in Timer.timers
    Timer.timers.clear()
    assert not Timer.timers
Beispiel #16
0
def test_timer_sets_last():
    with Timer() as t:
        time.sleep(0.02)
    assert t.last >= 0.01
Beispiel #17
0
def test_last_starts_as_nan():
    t = Timer()
    assert math.isnan(t.last)
Beispiel #18
0
def test_error_if_restarting_running_timer():
    t = Timer(text=TIME_MESSAGE)
    t.start()
    with pytest.raises(TimerError):
        t.start()