Search in sources :

Example 1 with BranchNoveltyFunction

use of org.evosuite.novelty.BranchNoveltyFunction 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;
}
Also used : BranchNoveltyFunction(org.evosuite.novelty.BranchNoveltyFunction) DiversityObserver(org.evosuite.testsuite.similarity.DiversityObserver) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) NoveltyFitnessEvaluationListener(org.evosuite.novelty.NoveltyFitnessEvaluationListener) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

BranchNoveltyFunction (org.evosuite.novelty.BranchNoveltyFunction)1 NoveltyFitnessEvaluationListener (org.evosuite.novelty.NoveltyFitnessEvaluationListener)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)1 DiversityObserver (org.evosuite.testsuite.similarity.DiversityObserver)1