Search in sources :

Example 16 with TestSuiteFitnessFunction

use of org.evosuite.testsuite.TestSuiteFitnessFunction 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)

Example 17 with TestSuiteFitnessFunction

use of org.evosuite.testsuite.TestSuiteFitnessFunction in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactory method readTestCases.

private void readTestCases() throws IllegalStateException {
    CarvingManager manager = CarvingManager.getInstance();
    final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
    List<TestCase> tests = manager.getTestsForClass(targetClass);
    junitTests.addAll(tests);
    if (junitTests.size() > 0) {
        totalNumberOfTestsCarved = junitTests.size();
        LoggingUtils.getEvoLogger().info("* Using {} carved tests from existing JUnit tests for seeding", junitTests.size());
        if (logger.isDebugEnabled()) {
            for (TestCase test : junitTests) {
                logger.debug("Carved Test: {}", test.toCode());
            }
        }
        TestSuiteChromosome suite = new TestSuiteChromosome();
        for (TestCase test : junitTests) {
            suite.addTest(test);
        }
        for (Properties.Criterion pc : Properties.CRITERION) {
            TestSuiteFitnessFunction f = FitnessFunctions.getFitnessFunction(pc);
            f.getFitness(suite);
        }
        carvedCoverage = suite.getCoverage();
    }
    ClientNodeLocal client = ClientServices.getInstance().getClientNode();
    client.trackOutputVariable(RuntimeVariable.CarvedTests, totalNumberOfTestsCarved);
    client.trackOutputVariable(RuntimeVariable.CarvedCoverage, carvedCoverage);
}
Also used : CarvingManager(org.evosuite.testcarver.extraction.CarvingManager) TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) ClientNodeLocal(org.evosuite.rmi.service.ClientNodeLocal) Properties(org.evosuite.Properties)

Aggregations

TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)17 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)11 ArrayList (java.util.ArrayList)6 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)6 TestChromosome (org.evosuite.testcase.TestChromosome)5 TestCase (org.evosuite.testcase.TestCase)3 HashSet (java.util.HashSet)2 Properties (org.evosuite.Properties)2 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)2 StoppingCondition (org.evosuite.ga.stoppingconditions.StoppingCondition)2 DiversityObserver (org.evosuite.testsuite.similarity.DiversityObserver)2 MIMEType (com.examples.with.different.packagename.concolic.MIMEType)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Criterion (org.evosuite.Properties.Criterion)1 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)1 MaxStatementsStoppingCondition (org.evosuite.ga.stoppingconditions.MaxStatementsStoppingCondition)1 MaxTestsStoppingCondition (org.evosuite.ga.stoppingconditions.MaxTestsStoppingCondition)1 BranchNoveltyFunction (org.evosuite.novelty.BranchNoveltyFunction)1 NoveltyFitnessEvaluationListener (org.evosuite.novelty.NoveltyFitnessEvaluationListener)1