use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class NoveltyStrategy method getGoals.
private List<TestFitnessFunction> getGoals(boolean verbose) {
List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
List<TestFitnessFunction> goals = new ArrayList<>();
if (goalFactories.size() == 1) {
TestFitnessFactory<? extends TestFitnessFunction> factory = goalFactories.iterator().next();
goals.addAll(factory.getCoverageGoals());
if (verbose) {
LoggingUtils.getEvoLogger().info("* Total number of test goals: {}", factory.getCoverageGoals().size());
if (Properties.PRINT_GOALS) {
for (TestFitnessFunction goal : factory.getCoverageGoals()) LoggingUtils.getEvoLogger().info("" + goal.toString());
}
}
} else {
if (verbose) {
LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
}
for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
goals.addAll(goalFactory.getCoverageGoals());
if (verbose) {
LoggingUtils.getEvoLogger().info(" - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
if (Properties.PRINT_GOALS) {
for (TestFitnessFunction goal : goalFactory.getCoverageGoals()) LoggingUtils.getEvoLogger().info("" + goal.toString());
}
}
}
}
return goals;
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class NoveltyStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
// Set up search algorithm
LoggingUtils.getEvoLogger().info("* Setting up search algorithm for novelty search");
PropertiesNoveltySearchFactory algorithmFactory = new PropertiesNoveltySearchFactory();
NoveltySearch<TestChromosome> algorithm = algorithmFactory.getSearchAlgorithm();
if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD)
TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(algorithm);
long startTime = System.currentTimeMillis() / 1000;
// What's the search target
List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
NoveltyFitnessEvaluationListener listener = new NoveltyFitnessEvaluationListener(fitnessFunctions);
algorithm.addListener(listener);
algorithm.setNoveltyFunction(new BranchNoveltyFunction());
if (Properties.TRACK_DIVERSITY)
algorithm.addListener(new DiversityObserver());
if (ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.DEFUSE) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.ALLDEFS) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.STATEMENT) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.RHO) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.AMBIGUITY))
ExecutionTracer.enableTraceCalls();
// TODO: why it was only if "analyzing"???
// if (analyzing)
algorithm.resetStoppingConditions();
List<TestFitnessFunction> goals = getGoals(true);
if (!canGenerateTestsForSUT()) {
LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
return new TestSuiteChromosome();
}
/*
* Proceed with search if CRITERION=EXCEPTION, even if goals is empty
*/
TestSuiteChromosome testSuite = null;
if (!(Properties.STOP_ZERO && goals.isEmpty()) || ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.EXCEPTION)) {
// Perform search
LoggingUtils.getEvoLogger().info("* Using seed {}", Randomness.getSeed());
LoggingUtils.getEvoLogger().info("* Starting evolution");
ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
algorithm.generateSolution();
testSuite = Archive.getArchiveInstance().mergeArchiveAndSolution(new TestSuiteChromosome());
} else {
zeroFitness.setFinished();
testSuite = new TestSuiteChromosome();
for (FitnessFunction<?> ff : fitnessFunctions) {
testSuite.setCoverage(ff, 1.0);
}
}
long endTime = System.currentTimeMillis() / 1000;
// recalculated now after the search, eg to handle exception fitness
goals = getGoals(false);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
// Newline after progress bar
if (Properties.SHOW_PROGRESS)
LoggingUtils.getEvoLogger().info("");
if (!Properties.IS_RUNNING_A_SYSTEM_TEST) {
// avoid printing time related info in system tests due to lack of determinism
LoggingUtils.getEvoLogger().info("* Search finished after " + (endTime - startTime) + "s and " + algorithm.getAge() + " generations, " + MaxStatementsStoppingCondition.getNumExecutedStatements() + " statements, best individual has fitness: " + testSuite.getFitness());
}
// Search is finished, send statistics
sendExecutionStatistics();
return testSuite;
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method addGoalsNotIncludedInTargetCriteria.
/**
* Name generation assumes that certain coverage criteria are included. If we haven't targeted them yet,
* we need to determine the covered goals. This may require re-executing the tests with observers.
*
* @param results
*/
private void addGoalsNotIncludedInTargetCriteria(List<ExecutionResult> results) {
List<Properties.Criterion> requiredCriteria = new ArrayList<>(Arrays.asList(new Properties.Criterion[] { Properties.Criterion.OUTPUT, Properties.Criterion.INPUT, Properties.Criterion.METHOD, Properties.Criterion.METHODNOEXCEPTION, Properties.Criterion.EXCEPTION }));
requiredCriteria.removeAll(Arrays.asList(Properties.CRITERION));
results = getUpdatedResults(requiredCriteria, results);
for (Properties.Criterion c : requiredCriteria) {
TestFitnessFactory<? extends TestFitnessFunction> goalFactory = FitnessFunctions.getFitnessFactory(c);
List<? extends TestFitnessFunction> goals = goalFactory.getCoverageGoals();
for (ExecutionResult result : results) {
for (TestFitnessFunction goal : goals) {
if (goal.isCovered(result))
result.test.addCoveredGoal(goal);
}
}
}
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method resolveAmbiguity.
/**
* Add additional goals for tests to testsToGoals to make them distinguishable
*
* @param testToGoals
* @param tests
* @param unique
* @return
*/
private boolean resolveAmbiguity(Map<TestCase, Set<TestFitnessFunction>> testToGoals, Set<TestCase> tests, boolean unique) {
// Full list of goals for given tests
Map<TestCase, Set<TestFitnessFunction>> fullTestToGoals = new LinkedHashMap<>();
for (TestCase test : tests) {
Set<TestFitnessFunction> goals = filterSupportedGoals(new LinkedHashSet<>(test.getCoveredGoals()));
// Remove those already used to name the test
goals.removeAll(testToGoals.get(test));
fullTestToGoals.put(test, goals);
}
// Find out what is unique about each one, or at least helps reduce the number of ambiguous names
if (unique)
findUniqueGoals(fullTestToGoals);
else
findNonUbiquitousGoals(fullTestToGoals);
// Select the next best goal from the new unique goal sets
boolean added = false;
for (TestCase test : tests) {
List<TestFitnessFunction> topGoals = getTopGoals(fullTestToGoals.get(test));
if (topGoals.isEmpty()) {
continue;
} else if (topGoals.size() > MAX_SIMILAR_GOALS) {
TestFitnessFunction newGoal = chooseRepresentativeGoal(test, topGoals);
Set<TestFitnessFunction> newGoals = new LinkedHashSet<>(testToGoals.get(test));
newGoals.add(newGoal);
String newName = getTestName(test, newGoals);
if (newName.length() < MAX_CHARS) {
if (testToGoals.get(test).add(newGoal))
added = true;
}
} else {
Set<TestFitnessFunction> newGoals = new LinkedHashSet<>(testToGoals.get(test));
Iterator<TestFitnessFunction> iterator = topGoals.iterator();
// getTestName(test, newGoals);
String newName = testToName.get(test);
while (newName.length() < MAX_CHARS && iterator.hasNext()) {
TestFitnessFunction newGoal = iterator.next();
newGoals.add(newGoal);
newName = getTestName(test, newGoals);
if (testToGoals.get(test).add(newGoal))
added = true;
}
}
}
return added;
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method chooseRepresentativeGoal.
/**
* Out of a set of multiple goals, select one that is representative.
* Assumes that goals is not empty, and all items in goals have the same type
* @param test
* @param goals
* @return
*/
private TestFitnessFunction chooseRepresentativeGoal(TestCase test, Collection<TestFitnessFunction> goals) {
Map<String, Integer> methodToPosition = new LinkedHashMap<>();
for (Statement st : test) {
if (st instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) st;
String name = ms.getMethodName() + ms.getDescriptor();
methodToPosition.put(name, st.getPosition());
} else if (st instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) st;
String name = "<init>" + cs.getDescriptor();
methodToPosition.put(name, st.getPosition());
}
}
// Randomness.choice(goals);
TestFitnessFunction chosenGoal = goals.iterator().next();
int chosenPosition = -1;
for (TestFitnessFunction goal : goals) {
if (methodToPosition.containsKey(goal.getTargetMethod())) {
int position = methodToPosition.get(goal.getTargetMethod());
if (position >= chosenPosition) {
chosenPosition = position;
chosenGoal = goal;
}
}
}
return chosenGoal;
}
Aggregations