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()
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 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"]
def test_error_if_restarting_running_timer(): t = Timer(text=TIME_MESSAGE) t.start() with pytest.raises(TimerError): t.start()