use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class PrimePathSuiteFitness method getFitness.
/* (non-Javadoc)
* @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
*/
/**
* {@inheritDoc}
*/
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
List<ExecutionResult> results = runTestSuite(suite);
List<TestFitnessFunction> coveredGoals = new ArrayList<TestFitnessFunction>();
double fitness = 0.0;
for (TestFitnessFunction goal : goals) {
double goalFitness = Double.MAX_VALUE;
for (ExecutionResult result : results) {
TestChromosome tc = new TestChromosome();
tc.setTestCase(result.test);
double resultFitness = goal.getFitness(tc, result);
if (resultFitness < goalFitness)
goalFitness = resultFitness;
if (goalFitness == 0.0) {
result.test.addCoveredGoal(goal);
coveredGoals.add(goal);
break;
}
}
fitness += goalFitness;
}
suite.setCoverage(this, coveredGoals.size() / (double) goals.size());
updateIndividual(this, suite, fitness);
return fitness;
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class DifferenceFitnessFunction method determineOriginalGoals.
/**
* Iterate over all coverage goals (e.g. branches) and determine which of
* these are covered only by the original test case
*
* @param factory
*/
private void determineOriginalGoals(TestFitnessFactory factory, List<TestCase> otherTests) {
// step 2
List<TestFitnessFunction> goals = factory.getCoverageGoals();
for (TestFitnessFunction goal : goals) {
if (goal.isCovered(originalTest)) {
if (!goal.isCovered(otherTests)) {
// This is a goal that is uniquely covered only by the target test
originalCoveredGoals.add(goal);
System.out.println("Uniquely covered goal: " + goal);
}
} else {
originalUncoveredGoals.add(goal);
}
}
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class TestCaseReplacer method replaceTest.
/**
* Given a test, create a GA and look for a replacement test
*
* @param test
*/
public TestCase replaceTest(String targetClass, List<TestCase> otherTests, TestCase test) {
// Various environmental setup necessary for EvoSuite
Properties.ALGORITHM = Algorithm.MONOTONICGA;
Properties.STRATEGY = Strategy.ONEBRANCH;
ExecutionTracer.enableTraceCalls();
// Run for 10 generations - adapt as necessary
// Properties.STOPPING_CONDITION = StoppingCondition.MAXGENERATIONS;
// Properties.SEARCH_BUDGET = 20;
// Properties.STOPPING_CONDITION = StoppingCondition.MAXTIME;
// Properties.SEARCH_BUDGET = 20;
// GeneticAlgorithm ga = TestSuiteGenerator.getGeneticAlgorithm(new RandomLengthTestFactory());
// TODO: JM: Needs Testing. Not sure if this is equivalent:
PropertiesTestGAFactory algorithmFactory = new PropertiesTestGAFactory();
GeneticAlgorithm<TestChromosome> ga = algorithmFactory.getSearchAlgorithm();
List<TestFitnessFactory<? extends TestFitnessFunction>> factories = TestSuiteGenerator.getFitnessFactories();
Collection<TestFitnessFunction> fitnessFunctions = new ArrayList<TestFitnessFunction>();
for (TestFitnessFactory<? extends TestFitnessFunction> factory : factories) {
// Set up fitness function for the parsed test case
DifferenceFitnessFunction fitnessFunction = new DifferenceFitnessFunction(test, otherTests, factory);
// ga.setFitnessFunction(fitness);
fitnessFunctions.add(fitnessFunction);
ga.addFitnessFunction(fitnessFunction);
}
// Perform calculation
ga.generateSolution();
// The best individual at the end of the search contains our candidate solution
TestChromosome testChromosome = (TestChromosome) ga.getBestIndividual();
TestCaseMinimizer minimizer = new TestCaseMinimizer(fitnessFunctions);
minimizer.minimize(testChromosome);
System.out.println("Best individual has fitness: " + testChromosome.getFitness());
return testChromosome.getTestCase();
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class TestSuiteGenerator method postProcessTests.
/**
* Apply any readability optimizations and other techniques that should use
* or modify the generated tests
*
* @param testSuite
*/
protected void postProcessTests(TestSuiteChromosome testSuite) {
// If overall time is short, the search might not have had enough time
// to come up with a suite without timeouts. However, they will slow
// down
// the rest of the process, and may lead to invalid tests
testSuite.getTestChromosomes().removeIf(t -> t.getLastExecutionResult() != null && (t.getLastExecutionResult().hasTimeout() || t.getLastExecutionResult().hasTestException()));
if (Properties.CTG_SEEDS_FILE_OUT != null) {
TestSuiteSerialization.saveTests(testSuite, new File(Properties.CTG_SEEDS_FILE_OUT));
} else if (Properties.TEST_FACTORY == TestFactory.SERIALIZATION) {
TestSuiteSerialization.saveTests(testSuite, new File(Properties.SEED_DIR + File.separator + Properties.TARGET_CLASS));
}
/*
* Remove covered goals that are not part of the minimization targets,
* as they might screw up coverage analysis when a minimization timeout
* occurs. This may happen e.g. when MutationSuiteFitness calls
* BranchCoverageSuiteFitness which adds branch goals.
*/
// TODO: This creates an inconsistency between
// suite.getCoveredGoals().size() and suite.getNumCoveredGoals()
// but it is not clear how to update numcoveredgoals
List<TestFitnessFunction> goals = new ArrayList<>();
for (TestFitnessFactory<?> ff : getFitnessFactories()) {
goals.addAll(ff.getCoverageGoals());
}
for (TestFitnessFunction f : testSuite.getCoveredGoals()) {
if (!goals.contains(f)) {
testSuite.removeCoveredGoal(f);
}
}
if (Properties.INLINE) {
ClientServices.getInstance().getClientNode().changeState(ClientState.INLINING);
ConstantInliner inliner = new ConstantInliner();
// progressMonitor.setCurrentPhase("Inlining constants");
// Map<FitnessFunction<? extends TestSuite<?>>, Double> fitnesses =
// testSuite.getFitnesses();
inliner.inline(testSuite);
}
if (Properties.MINIMIZE) {
ClientServices.getInstance().getClientNode().changeState(ClientState.MINIMIZATION);
// progressMonitor.setCurrentPhase("Minimizing test cases");
if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
LoggingUtils.getEvoLogger().info("* Skipping minimization because not enough time is left");
ClientServices.track(RuntimeVariable.Result_Size, testSuite.size());
ClientServices.track(RuntimeVariable.Minimized_Size, testSuite.size());
ClientServices.track(RuntimeVariable.Result_Length, testSuite.totalLengthOfTestCases());
ClientServices.track(RuntimeVariable.Minimized_Length, testSuite.totalLengthOfTestCases());
} else if (Properties.isRegression()) {
RegressionSuiteMinimizer minimizer = new RegressionSuiteMinimizer();
minimizer.minimize(testSuite);
} else {
double before = testSuite.getFitness();
TestSuiteMinimizer minimizer = new TestSuiteMinimizer(getFitnessFactories());
LoggingUtils.getEvoLogger().info("* Minimizing test suite");
minimizer.minimize(testSuite, true);
double after = testSuite.getFitness();
if (after > before + 0.01d) {
// assume minimization
throw new Error("EvoSuite bug: minimization lead fitness from " + before + " to " + after);
}
}
} else {
if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
LoggingUtils.getEvoLogger().info("* Skipping minimization because not enough time is left");
}
ClientServices.track(RuntimeVariable.Result_Size, testSuite.size());
ClientServices.track(RuntimeVariable.Minimized_Size, testSuite.size());
ClientServices.track(RuntimeVariable.Result_Length, testSuite.totalLengthOfTestCases());
ClientServices.track(RuntimeVariable.Minimized_Length, testSuite.totalLengthOfTestCases());
}
if (Properties.COVERAGE) {
ClientServices.getInstance().getClientNode().changeState(ClientState.COVERAGE_ANALYSIS);
CoverageCriteriaAnalyzer.analyzeCoverage(testSuite);
}
double coverage = testSuite.getCoverage();
if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
// SearchStatistics.getInstance().mutationScore(coverage);
}
StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
LoggingUtils.getEvoLogger().info("* Generated " + testSuite.size() + " tests with total length " + testSuite.totalLengthOfTestCases());
// TODO: In the end we will only need one analysis technique
if (!Properties.ANALYSIS_CRITERIA.isEmpty()) {
// SearchStatistics.getInstance().addCoverage(Properties.CRITERION.toString(),
// coverage);
CoverageCriteriaAnalyzer.analyzeCriteria(testSuite, Properties.ANALYSIS_CRITERIA);
// FIXME: can we send all bestSuites?
}
if (Properties.CRITERION.length > 1)
LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage) + " (average coverage for all fitness functions)");
else
LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage));
// printBudget(ga); // TODO - need to move this somewhere else
if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) && Properties.ANALYSIS_CRITERIA.isEmpty())
DefUseCoverageSuiteFitness.printCoverage();
DSEStats.getInstance().trackConstraintTypes();
DSEStats.getInstance().trackSolverStatistics();
if (Properties.DSE_PROBABILITY > 0.0 && Properties.LOCAL_SEARCH_RATE > 0 && Properties.LOCAL_SEARCH_PROBABILITY > 0.0) {
DSEStats.getInstance().logStatistics();
}
if (Properties.FILTER_SANDBOX_TESTS) {
for (TestChromosome test : testSuite.getTestChromosomes()) {
// delete all statements leading to security exceptions
ExecutionResult result = test.getLastExecutionResult();
if (result == null) {
result = TestCaseExecutor.runTest(test.getTestCase());
}
if (result.hasSecurityException()) {
int position = result.getFirstPositionOfThrownException();
if (position > 0) {
test.getTestCase().chop(position);
result = TestCaseExecutor.runTest(test.getTestCase());
test.setLastExecutionResult(result);
}
}
}
}
if (Properties.ASSERTIONS && !Properties.isRegression()) {
LoggingUtils.getEvoLogger().info("* Generating assertions");
// progressMonitor.setCurrentPhase("Generating assertions");
ClientServices.getInstance().getClientNode().changeState(ClientState.ASSERTION_GENERATION);
if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
LoggingUtils.getEvoLogger().info("* Skipping assertion generation because not enough time is left");
} else {
TestSuiteGeneratorHelper.addAssertions(testSuite);
}
// FIXME: can we
StatisticsSender.sendIndividualToMaster(testSuite);
// pass the list
// of
// testsuitechromosomes?
}
if (Properties.NO_RUNTIME_DEPENDENCY) {
LoggingUtils.getEvoLogger().info("* Property NO_RUNTIME_DEPENDENCY is set to true - skipping JUnit compile check");
LoggingUtils.getEvoLogger().info("* WARNING: Not including the runtime dependencies is likely to lead to flaky tests!");
} else if (Properties.JUNIT_TESTS && Properties.JUNIT_CHECK) {
compileAndCheckTests(testSuite);
}
if (Properties.SERIALIZE_REGRESSION_TEST_SUITE) {
RegressionSuiteSerializer.appendToRegressionTestSuite(testSuite);
}
if (Properties.isRegression() && Properties.KEEP_REGRESSION_ARCHIVE) {
RegressionSuiteSerializer.storeRegressionArchive();
}
}
use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.
the class AllDefsCoverageSuiteFitness method getFitness.
/*
* (non-Javadoc)
*
* @see
* org.evosuite.ga.FitnessFunction#getFitness(org.
* evosuite.ga.Chromosome)
*/
/**
* {@inheritDoc}
*/
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
logger.trace("Calculating defuse fitness");
TestSuiteChromosome suite = (TestSuiteChromosome) individual;
List<ExecutionResult> results = runTestSuite(suite);
double fitness = 0.0;
Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>();
for (TestFitnessFunction goal : goals) {
if (coveredGoals.contains(goal))
continue;
double goalFitness = 2.0;
for (ExecutionResult result : results) {
TestChromosome tc = new TestChromosome();
tc.setTestCase(result.test);
double resultFitness = goal.getFitness(tc, result);
if (resultFitness < goalFitness)
goalFitness = resultFitness;
if (goalFitness == 0.0) {
result.test.addCoveredGoal(goal);
// System.out.println(goal.toString());
// System.out.println(result.test.toCode());
// System.out.println(resultFitness);
coveredGoals.add(goal);
break;
}
}
fitness += goalFitness;
}
updateIndividual(this, individual, fitness);
setSuiteCoverage(suite, coveredGoals);
return fitness;
}
Aggregations