use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.
the class DSEStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
LoggingUtils.getEvoLogger().info("* Setting up DSE test suite generation");
long startTime = System.currentTimeMillis() / 1000;
Properties.CRITERION = new Criterion[] { Properties.Criterion.BRANCH };
// What's the search target
List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
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, Criterion.EXCEPTION)) {
// Perform search
LoggingUtils.getEvoLogger().info("* Using seed {}", Randomness.getSeed());
LoggingUtils.getEvoLogger().info("* Starting evolution");
ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
testSuite = generateSuite();
} 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
goals = getGoals(false);
// handle exception fitness
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 " + MaxStatementsStoppingCondition.getNumExecutedStatements() + " statements, best individual has fitness: " + testSuite.getFitness());
}
// Search is finished, send statistics
sendExecutionStatistics();
return testSuite;
}
use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.
the class RegressionSuiteStrategy method generateTests.
@Override
public TestSuiteChromosome generateTests() {
track(RuntimeVariable.Total_Goals, 0);
track(RuntimeVariable.Generated_Assertions, 0);
track(RuntimeVariable.Coverage_Old, 0);
track(RuntimeVariable.Coverage_New, 0);
track(RuntimeVariable.Exception_Difference, 0);
track(RuntimeVariable.State_Distance, 0);
track(RuntimeVariable.Testsuite_Diversity, 0);
// Disable test archive
Properties.TEST_ARCHIVE = false;
// Disable functional mocking stuff (due to incompatibilities)
Properties.P_FUNCTIONAL_MOCKING = 0;
Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT = 0;
Properties.FUNCTIONAL_MOCKING_PERCENT = 0;
// Regression random strategy switch.
if (Properties.REGRESSION_FITNESS == RegressionMeasure.RANDOM) {
return generateRandomRegressionTests();
}
LoggingUtils.getEvoLogger().info("* Setting up search algorithm for REGRESSION suite generation");
PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
GeneticAlgorithm<?> algorithm = algorithmFactory.getSearchAlgorithm();
if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD) {
TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(algorithm);
}
long startTime = System.currentTimeMillis() / 1000;
Properties.CRITERION = new Criterion[] { Criterion.REGRESSION };
// What's the search target
List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
// TODO: Argh, generics.
algorithm.addFitnessFunctions((List) fitnessFunctions);
if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) || ArrayUtil.contains(Properties.CRITERION, Criterion.ALLDEFS) || ArrayUtil.contains(Properties.CRITERION, Criterion.STATEMENT) || ArrayUtil.contains(Properties.CRITERION, Criterion.RHO) || ArrayUtil.contains(Properties.CRITERION, Criterion.AMBIGUITY)) {
ExecutionTracer.enableTraceCalls();
}
// TODO: why it was only if "analyzing"???
// if (analyzing)
algorithm.resetStoppingConditions();
List<TestFitnessFunction> goals = getGoals(true);
// List<TestSuiteChromosome> bestSuites = new
// ArrayList<TestSuiteChromosome>();
TestSuiteChromosome bestSuites = new TestSuiteChromosome();
RegressionTestSuiteChromosome best = null;
if (!Properties.STOP_ZERO || !goals.isEmpty()) {
// logger.warn("performing search ... ############################################################");
// Perform search
LoggingUtils.getEvoLogger().info("* Using seed {}", Randomness.getSeed());
LoggingUtils.getEvoLogger().info("* Starting evolution");
ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
algorithm.generateSolution();
best = (RegressionTestSuiteChromosome) algorithm.getBestIndividual();
// ArrayList<TestSuiteChromosome>();
for (TestCase t : best.getTests()) {
bestSuites.addTest(t);
}
// bestSuites = (List<TestSuiteChromosome>) ga.getBestIndividuals();
if (bestSuites.size() == 0) {
LoggingUtils.getEvoLogger().warn("Could not find any suiteable chromosome");
return bestSuites;
}
} else {
zeroFitness.setFinished();
bestSuites = new TestSuiteChromosome();
for (FitnessFunction<?> ff : bestSuites.getFitnessValues().keySet()) {
bestSuites.setCoverage(ff, 1.0);
}
}
long end_time = System.currentTimeMillis() / 1000;
// recalculated now after the search, eg to handle exception fitness
goals = getGoals(false);
track(RuntimeVariable.Total_Goals, goals.size());
// Newline after progress bar
if (Properties.SHOW_PROGRESS) {
LoggingUtils.getEvoLogger().info("");
}
String text = " statements, best individual has fitness: ";
if (bestSuites.size() > 1) {
text = " statements, best individuals have fitness: ";
}
LoggingUtils.getEvoLogger().info("* Search finished after " + (end_time - startTime) + "s and " + algorithm.getAge() + " generations, " + MaxStatementsStoppingCondition.getNumExecutedStatements() + text + ((best != null) ? best.getFitness() : ""));
if (Properties.COVERAGE) {
for (Properties.Criterion pc : Properties.CRITERION) {
// FIXME: can
CoverageCriteriaAnalyzer.analyzeCoverage(bestSuites, pc);
}
// we send
// all
// bestSuites?
}
// progressMonitor.updateStatus(99);
int number_of_test_cases = 0;
int totalLengthOfTestCases = 0;
double coverage = 0.0;
// for (TestSuiteChromosome tsc : bestSuites) {
number_of_test_cases += bestSuites.size();
totalLengthOfTestCases += bestSuites.totalLengthOfTestCases();
coverage += bestSuites.getCoverage();
if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
// SearchStatistics.getInstance().mutationScore(coverage);
}
// StatisticsSender.executedAndThenSendIndividualToMaster(bestSuites);
// // FIXME: can we send all bestSuites?
// statistics.iteration(ga);
// statistics.minimized(bestSuites.get(0)); // FIXME: can we send all
// bestSuites?
LoggingUtils.getEvoLogger().info("* Generated " + number_of_test_cases + " tests with total length " + 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(bestSuites, Properties.ANALYSIS_CRITERIA);
// FIXME: can we send all bestSuites?
}
LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage));
algorithm.printBudget();
return bestSuites;
}
use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.
the class TestLocalSearchMIMEType method testFitness.
@Test
public void testFitness() throws NoSuchFieldException, SecurityException, NoSuchMethodException, ClassNotFoundException {
Properties.RESET_STATIC_FINAL_FIELDS = false;
Properties.TEST_ARCHIVE = false;
Properties.LOCAL_SEARCH_PROBABILITY = 1.0;
Properties.LOCAL_SEARCH_RATE = 1;
Properties.LOCAL_SEARCH_BUDGET_TYPE = Properties.LocalSearchBudgetType.TESTS;
Properties.LOCAL_SEARCH_BUDGET = 100;
Properties.DSE_SOLVER = SolverType.EVOSUITE_SOLVER;
Properties.STOPPING_CONDITION = StoppingCondition.MAXTIME;
Properties.SEARCH_BUDGET = 120;
Properties.TARGET_CLASS = MIMEType.class.getName();
String classPath = ClassPathHandler.getInstance().getTargetProjectClasspath();
DependencyAnalysis.analyzeClass(MIMEType.class.getName(), Arrays.asList(classPath));
TestSuiteChromosome suite = new TestSuiteChromosome();
DefaultTestCase test0 = createTestCase0();
DefaultTestCase test1 = createTestCase1();
DefaultTestCase test2 = createTestCase2();
DefaultTestCase test3 = createTestCase3();
DefaultTestCase test4 = createTestCase4();
DefaultTestCase test5 = createTestCase5();
suite.addTest(test0);
suite.addTest(test1);
suite.addTest(test2);
suite.addTest(test3);
suite.addTest(test4);
suite.addTest(test5);
TestSuiteFitnessFunction lineCoverage = FitnessFunctions.getFitnessFunction(Criterion.LINE);
TestSuiteFitnessFunction branchCoverage = FitnessFunctions.getFitnessFunction(Criterion.BRANCH);
TestSuiteFitnessFunction exceptionCoverage = FitnessFunctions.getFitnessFunction(Criterion.EXCEPTION);
TestSuiteFitnessFunction weakMutationCoverage = FitnessFunctions.getFitnessFunction(Criterion.WEAKMUTATION);
TestSuiteFitnessFunction outputCoverage = FitnessFunctions.getFitnessFunction(Criterion.OUTPUT);
TestSuiteFitnessFunction methodCoverage = FitnessFunctions.getFitnessFunction(Criterion.METHOD);
TestSuiteFitnessFunction methodNoExceptionCoverage = FitnessFunctions.getFitnessFunction(Criterion.METHODNOEXCEPTION);
TestSuiteFitnessFunction cbranchCoverage = FitnessFunctions.getFitnessFunction(Criterion.CBRANCH);
List<TestSuiteFitnessFunction> fitnessFunctions = new ArrayList<TestSuiteFitnessFunction>();
fitnessFunctions.add(lineCoverage);
fitnessFunctions.add(branchCoverage);
fitnessFunctions.add(exceptionCoverage);
fitnessFunctions.add(weakMutationCoverage);
fitnessFunctions.add(outputCoverage);
fitnessFunctions.add(methodCoverage);
fitnessFunctions.add(methodNoExceptionCoverage);
fitnessFunctions.add(cbranchCoverage);
for (TestSuiteFitnessFunction ff : fitnessFunctions) {
suite.addFitness(ff);
}
for (TestSuiteFitnessFunction ff : fitnessFunctions) {
double oldFitness = ff.getFitness(suite);
System.out.println(ff.toString() + "->" + oldFitness);
}
double oldFitness = suite.getFitness();
System.out.println("oldFitness->" + oldFitness);
System.out.println("oldSize->" + suite.getTests().size());
DefaultLocalSearchObjective objective = new DefaultLocalSearchObjective<>();
for (TestSuiteFitnessFunction ff : fitnessFunctions) {
objective.addFitnessFunction(ff);
}
boolean hasImproved = suite.localSearch(objective);
System.out.println("hasImproved=" + hasImproved);
for (TestSuiteFitnessFunction ff : fitnessFunctions) {
double newFitness = ff.getFitness(suite);
System.out.println(ff.toString() + "->" + newFitness);
}
double newFitness = suite.getFitness();
System.out.println("newFitness->" + newFitness);
System.out.println("newSize->" + suite.getTests().size());
assertTrue(newFitness <= oldFitness);
}
use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.
the class MOSA method getBestIndividual.
/**
* This method is used by the Progress Monitor at the and of each generation to show the totol coverage reached by the algorithm.
* Since the Progress Monitor need a "Suite", this method artificially creates a "SuiteChromosome" (see {@link MOSA#suiteFitness})
* as the union of all test cases stored in {@link MOSA#archive}.
*
* The coverage score of the "SuiteChromosome" is given by the percentage of test goals covered (goals in {@link MOSA#archive})
* onto the total number of goals <code> this.fitnessFunctions</code> (see {@link GeneticAlgorithm}).
*
* @return "SuiteChromosome" directly consumable by the Progress Monitor.
*/
@Override
@SuppressWarnings("unchecked")
public T getBestIndividual() {
List<T> archiveContent = this.getArchive();
if (archiveContent.isEmpty()) {
return (T) new TestSuiteChromosome();
}
TestSuiteChromosome best = new TestSuiteChromosome();
for (T test : archiveContent) {
best.addTest((TestChromosome) test);
}
// compute overall fitness and coverage
double coverage = ((double) this.getNumberOfCoveredGoals()) / ((double) this.fitnessFunctions.size());
for (TestSuiteFitnessFunction suiteFitness : suiteFitnesses) {
best.setCoverage(suiteFitness, coverage);
best.setFitness(suiteFitness, this.fitnessFunctions.size() - this.getNumberOfCoveredGoals());
}
// suiteFitness.getFitness(best);
return (T) best;
}
use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.
the class AbstractMOSA method getBestIndividuals.
@SuppressWarnings("unchecked")
@Override
public List<T> getBestIndividuals() {
// get final test suite (i.e., non dominated solutions in Archive)
List<T> finalTestSuite = this.getFinalTestSuite();
if (finalTestSuite.isEmpty()) {
return Arrays.asList((T) new TestSuiteChromosome());
}
TestSuiteChromosome bestTestCases = new TestSuiteChromosome();
for (T test : finalTestSuite) {
bestTestCases.addTest((TestChromosome) test);
}
for (FitnessFunction<T> f : this.getCoveredGoals()) {
bestTestCases.getCoveredGoals().add((TestFitnessFunction) f);
}
// compute overall fitness and coverage
double fitness = this.fitnessFunctions.size() - numberOfCoveredTargets();
double coverage = ((double) numberOfCoveredTargets()) / ((double) this.fitnessFunctions.size());
for (TestSuiteFitnessFunction suiteFitness : suiteFitnesses) {
bestTestCases.setFitness(suiteFitness, fitness);
bestTestCases.setCoverage(suiteFitness, coverage);
bestTestCases.setNumOfCoveredGoals(suiteFitness, (int) numberOfCoveredTargets());
bestTestCases.setNumOfNotCoveredGoals(suiteFitness, (int) (this.fitnessFunctions.size() - numberOfCoveredTargets()));
}
List<T> bests = new ArrayList<T>(1);
bests.add((T) bestTestCases);
return bests;
}
Aggregations