use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class RandomTestStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
LoggingUtils.getEvoLogger().info("* Using random test generation");
List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
TestSuiteChromosome suite = new TestSuiteChromosome();
for (TestSuiteFitnessFunction fitnessFunction : fitnessFunctions) suite.addFitness(fitnessFunction);
List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>();
LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
goals.addAll(goalFactory.getCoverageGoals());
LoggingUtils.getEvoLogger().info(" - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
}
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
if (!canGenerateTestsForSUT()) {
LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
return new TestSuiteChromosome();
}
ChromosomeFactory<TestChromosome> factory = getChromosomeFactory();
StoppingCondition stoppingCondition = getStoppingCondition();
for (FitnessFunction<?> fitness_function : fitnessFunctions) ((TestSuiteFitnessFunction) fitness_function).getFitness(suite);
ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
int number_generations = 0;
while (!isFinished(suite, stoppingCondition)) {
number_generations++;
TestChromosome test = factory.getChromosome();
TestSuiteChromosome clone = suite.clone();
clone.addTest(test);
for (FitnessFunction<?> fitness_function : fitnessFunctions) {
((TestSuiteFitnessFunction) fitness_function).getFitness(clone);
logger.debug("Old fitness: {}, new fitness: {}", suite.getFitness(), clone.getFitness());
}
if (clone.compareTo(suite) < 0) {
suite = clone;
StatisticsSender.executedAndThenSendIndividualToMaster(clone);
}
}
// statistics.searchFinished(suiteGA);
LoggingUtils.getEvoLogger().info("* Search Budget:");
LoggingUtils.getEvoLogger().info("\t- " + stoppingCondition.toString());
// In the GA, these statistics are sent via the SearchListener when notified about the GA completing
// Search is finished, send statistics
sendExecutionStatistics();
// TODO: Check this: Fitness_Evaluations = getNumExecutedTests?
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Fitness_Evaluations, MaxTestsStoppingCondition.getNumExecutedTests());
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Generations, number_generations);
return suite;
}
use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class IndividualTestStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
// Set up search algorithm
LoggingUtils.getEvoLogger().info("* Setting up search algorithm for individual test generation");
ExecutionTracer.enableTraceCalls();
PropertiesTestGAFactory factory = new PropertiesTestGAFactory();
List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
long start_time = System.currentTimeMillis() / 1000;
// Get list of goals
List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
// long goalComputationStart = System.currentTimeMillis();
List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>();
LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
goals.addAll(goalFactory.getCoverageGoals());
LoggingUtils.getEvoLogger().info(" - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
}
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();
}
// Need to shuffle goals because the order may make a difference
if (Properties.SHUFFLE_GOALS) {
// LoggingUtils.getEvoLogger().info("* Shuffling goals");
Randomness.shuffle(goals);
}
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
LoggingUtils.getEvoLogger().info("* Total number of test goals: " + goals.size());
// Bootstrap with random testing to cover easy goals
// statistics.searchStarted(suiteGA);
StoppingCondition stoppingCondition = getStoppingCondition();
// FIXME: just one fitness and one factory?!
TestSuiteChromosome suite = (TestSuiteChromosome) bootstrapRandomSuite(fitnessFunctions.get(0), goalFactories.get(0));
Set<Integer> covered = new HashSet<Integer>();
int covered_goals = 0;
int num = 0;
for (TestFitnessFunction fitness_function : goals) {
if (fitness_function.isCoveredBy(suite)) {
covered.add(num);
covered_goals++;
}
num++;
}
if (covered_goals > 0)
LoggingUtils.getEvoLogger().info("* Random bootstrapping covered " + covered_goals + " test goals");
int total_goals = goals.size();
if (covered_goals == total_goals)
zeroFitness.setFinished();
int current_budget = 0;
long total_budget = Properties.SEARCH_BUDGET;
LoggingUtils.getEvoLogger().info("* Budget: " + NumberFormat.getIntegerInstance().format(total_budget));
while (current_budget < total_budget && covered_goals < total_goals && !globalTime.isFinished() && !ShutdownTestWriter.isInterrupted()) {
long budget = (total_budget - current_budget) / (total_goals - covered_goals);
logger.info("Budget: " + budget + "/" + (total_budget - current_budget));
logger.info("Statements: " + current_budget + "/" + total_budget);
logger.info("Goals covered: " + covered_goals + "/" + total_goals);
stoppingCondition.setLimit(budget);
num = 0;
// //MaxStatementsStoppingCondition.getNumExecutedStatements();
for (TestFitnessFunction fitnessFunction : goals) {
if (covered.contains(num)) {
num++;
continue;
}
GeneticAlgorithm<TestChromosome> ga = factory.getSearchAlgorithm();
if (Properties.PRINT_CURRENT_GOALS)
LoggingUtils.getEvoLogger().info("* Searching for goal " + num + ": " + fitnessFunction.toString());
logger.info("Goal " + num + "/" + (total_goals - covered_goals) + ": " + fitnessFunction);
if (ShutdownTestWriter.isInterrupted()) {
num++;
continue;
}
if (globalTime.isFinished()) {
LoggingUtils.getEvoLogger().info("Skipping goal because time is up");
num++;
continue;
}
// FitnessFunction fitness_function = new
ga.addFitnessFunction(fitnessFunction);
// Perform search
logger.info("Starting evolution for goal " + fitnessFunction);
ga.generateSolution();
if (ga.getBestIndividual().getFitness() == 0.0) {
if (Properties.PRINT_COVERED_GOALS)
// : " +
LoggingUtils.getEvoLogger().info("* Covered!");
// fitness_function.toString());
logger.info("Found solution, adding to test suite at " + MaxStatementsStoppingCondition.getNumExecutedStatements());
TestChromosome best = (TestChromosome) ga.getBestIndividual();
best.getTestCase().addCoveredGoal(fitnessFunction);
suite.addTest(best);
// Calculate and keep track of overall fitness
for (TestSuiteFitnessFunction fitness_function : fitnessFunctions) fitness_function.getFitness(suite);
covered_goals++;
covered.add(num);
// experiment:
if (Properties.SKIP_COVERED) {
Set<Integer> additional_covered_nums = getAdditionallyCoveredGoals(goals, covered, best);
// LoggingUtils.getEvoLogger().info("Additionally covered: "+additional_covered_nums.size());
for (Integer covered_num : additional_covered_nums) {
covered_goals++;
covered.add(covered_num);
}
}
} else {
logger.info("Found no solution for " + fitnessFunction + " at " + MaxStatementsStoppingCondition.getNumExecutedStatements());
}
// statistics.iteration(suiteGA);
if (Properties.REUSE_BUDGET)
current_budget += stoppingCondition.getCurrentValue();
else
current_budget += budget + 1;
// print console progress bar
if (Properties.SHOW_PROGRESS && !(Properties.PRINT_COVERED_GOALS || Properties.PRINT_CURRENT_GOALS)) {
double percent = current_budget;
percent = percent / total_budget * 100;
double coverage = covered_goals;
coverage = coverage / total_goals * 100;
// ConsoleProgressBar.printProgressBar((int) percent, (int)
// coverage);
}
if (current_budget > total_budget)
break;
num++;
// break;
}
}
if (Properties.SHOW_PROGRESS)
LoggingUtils.getEvoLogger().info("");
// for testing purposes
if (globalTime.isFinished())
LoggingUtils.getEvoLogger().info("! Timeout reached");
if (current_budget >= total_budget)
LoggingUtils.getEvoLogger().info("! Budget exceeded");
else
LoggingUtils.getEvoLogger().info("* Remaining budget: " + (total_budget - current_budget));
// stoppingCondition.setLimit(Properties.SEARCH_BUDGET);
// stoppingCondition.forceCurrentValue(current_budget);
// suiteGA.setStoppingCondition(stopping_condition);
// suiteGA.addStoppingCondition(global_time);
// printBudget(suiteGA);
int c = 0;
int uncovered_goals = total_goals - covered_goals;
if (uncovered_goals < 10)
for (TestFitnessFunction goal : goals) {
if (!covered.contains(c)) {
LoggingUtils.getEvoLogger().info("! Unable to cover goal " + c + " " + goal.toString());
}
c++;
}
else
LoggingUtils.getEvoLogger().info("! #Goals that were not covered: " + uncovered_goals);
// statistics.searchFinished(suiteGA);
long end_time = System.currentTimeMillis() / 1000;
LoggingUtils.getEvoLogger().info("* Search finished after " + (end_time - start_time) + "s, " + current_budget + " statements, best individual has fitness " + suite.getFitness());
// Search is finished, send statistics
sendExecutionStatistics();
LoggingUtils.getEvoLogger().info("* Covered " + covered_goals + "/" + goals.size() + " goals");
logger.info("Resulting test suite: " + suite.size() + " tests, length " + suite.totalLengthOfTestCases());
return suite;
}
use of org.evosuite.coverage.TestFitnessFactory 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.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class TestTestSuiteMinimizer method minimizeSuiteFullCoverageWithTwoFitnessFunctionsMinimizeTestsEnabled.
@Test
public void minimizeSuiteFullCoverageWithTwoFitnessFunctionsMinimizeTestsEnabled() throws ClassNotFoundException, NoSuchFieldException, SecurityException, ConstructionFailedException, NoSuchMethodException {
Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
testFactory.addConstructor(test, gc, 0, 0);
List<VariableReference> parameters = new ArrayList<VariableReference>();
for (int i = 0; i < 10; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
VariableReference vr = test.addStatement(ips, i + 1);
}
ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
GenericMethod method = new GenericMethod(m, sut);
testFactory.addMethod(test, method, 11, 0);
parameters = new ArrayList<VariableReference>();
for (int i = 12; i < 15; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i);
VariableReference vr = test.addStatement(ips, i);
}
ct = new ConstructorStatement(test, gc, parameters);
testFactory.addMethod(test, method, 15, 0);
assertEquals(16, test.size());
TestSuiteChromosome tsc = new TestSuiteChromosome();
tsc.addTest(test);
TestSuiteFitnessFunction branch = new BranchCoverageSuiteFitness();
double previous_branch_fitness = branch.getFitness(tsc);
tsc.setFitness(branch, previous_branch_fitness);
assertEquals(previous_branch_fitness, 0.0, 0.0);
TestSuiteFitnessFunction defuse = new DefUseCoverageSuiteFitness();
double previous_defuse_fitness = defuse.getFitness(tsc);
tsc.setFitness(defuse, previous_defuse_fitness);
assertEquals(previous_defuse_fitness, 0.0, 0.0);
List<TestFitnessFactory<? extends TestFitnessFunction>> factories = new ArrayList<TestFitnessFactory<? extends TestFitnessFunction>>();
factories.add(new BranchCoverageFactory());
factories.add(new DefUseCoverageFactory());
TestSuiteMinimizer minimizer = new TestSuiteMinimizer(factories);
minimizer.minimize(tsc, true);
assertEquals(2, tsc.getTests().size());
assertEquals(3, tsc.getTests().get(0).size());
assertEquals(3, tsc.getTests().get(1).size());
// assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nflagExample1_0.testMe(int0);\n"));
// assertTrue(tsc.getTests().get(1).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28241;\nflagExample1_0.testMe(int0);\n"));
double branch_fitness = branch.getFitness(tsc);
assertEquals(previous_branch_fitness, branch_fitness, 0.0);
double defuse_fitness = defuse.getFitness(tsc);
assertEquals(previous_defuse_fitness, defuse_fitness, 0.0);
}
use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.
the class TestTestSuiteMinimizer method minimizeSuiteFullCoverageWithTwoFitnessFunctions.
@Test
public void minimizeSuiteFullCoverageWithTwoFitnessFunctions() throws ClassNotFoundException, NoSuchFieldException, SecurityException, ConstructionFailedException, NoSuchMethodException {
Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
testFactory.addConstructor(test, gc, 0, 0);
List<VariableReference> parameters = new ArrayList<VariableReference>();
for (int i = 0; i < 10; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
VariableReference vr = test.addStatement(ips, i + 1);
}
ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
GenericMethod method = new GenericMethod(m, sut);
testFactory.addMethod(test, method, 11, 0);
parameters = new ArrayList<VariableReference>();
for (int i = 12; i < 15; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i);
VariableReference vr = test.addStatement(ips, i);
}
ct = new ConstructorStatement(test, gc, parameters);
testFactory.addMethod(test, method, 15, 0);
assertEquals(16, test.size());
TestSuiteChromosome tsc = new TestSuiteChromosome();
tsc.addTest(test);
TestSuiteFitnessFunction branch = new BranchCoverageSuiteFitness();
double previous_branch_fitness = branch.getFitness(tsc);
tsc.setFitness(branch, previous_branch_fitness);
assertEquals(previous_branch_fitness, 0.0, 0.0);
TestSuiteFitnessFunction defuse = new DefUseCoverageSuiteFitness();
double previous_defuse_fitness = defuse.getFitness(tsc);
tsc.setFitness(defuse, previous_defuse_fitness);
assertEquals(previous_defuse_fitness, 0.0, 0.0);
List<TestFitnessFactory<? extends TestFitnessFunction>> factories = new ArrayList<TestFitnessFactory<? extends TestFitnessFunction>>();
factories.add(new BranchCoverageFactory());
factories.add(new DefUseCoverageFactory());
TestSuiteMinimizer minimizer = new TestSuiteMinimizer(factories);
minimizer.minimize(tsc, false);
assertEquals(1, tsc.getTests().size());
assertEquals(5, tsc.getTests().get(0).size());
// assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nint int1 = 28241;\nflagExample1_0.testMe(int1);\nflagExample1_0.testMe(int0);\n"));
double branch_fitness = branch.getFitness(tsc);
assertEquals(previous_branch_fitness, branch_fitness, 0.0);
double defuse_fitness = defuse.getFitness(tsc);
assertEquals(previous_defuse_fitness, defuse_fitness, 0.0);
}
Aggregations