Search in sources :

Example 1 with ChromosomeFactory

use of org.evosuite.ga.ChromosomeFactory in project evosuite by EvoSuite.

the class MOSuiteStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    // Set up search algorithm
    PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
    GeneticAlgorithm<TestSuiteChromosome> algorithm = algorithmFactory.getSearchAlgorithm();
    // Override chromosome factory
    // TODO handle this better by introducing generics
    ChromosomeFactory factory = new RandomLengthTestFactory();
    algorithm.setChromosomeFactory(factory);
    if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD)
        TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(algorithm);
    long startTime = System.currentTimeMillis() / 1000;
    // What's the search target
    List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
    List<TestFitnessFunction> fitnessFunctions = new ArrayList<TestFitnessFunction>();
    for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
        fitnessFunctions.addAll(goalFactory.getCoverageGoals());
    }
    algorithm.addFitnessFunctions((List) fitnessFunctions);
    // if (Properties.SHOW_PROGRESS && !logger.isInfoEnabled())
    // FIXME progressMonitor may cause
    algorithm.addListener(progressMonitor);
    if (Properties.ALGORITHM == Properties.Algorithm.LIPS)
        LoggingUtils.getEvoLogger().info("* Total number of test goals for LIPS: {}", fitnessFunctions.size());
    else if (Properties.ALGORITHM == Properties.Algorithm.MOSA)
        LoggingUtils.getEvoLogger().info("* Total number of test goals for MOSA: {}", fitnessFunctions.size());
    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.BRANCH) || ArrayUtil.contains(Properties.CRITERION, Criterion.AMBIGUITY))
        ExecutionTracer.enableTraceCalls();
    algorithm.resetStoppingConditions();
    TestSuiteChromosome testSuite = null;
    if (!(Properties.STOP_ZERO && fitnessFunctions.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);
        algorithm.generateSolution();
        List<TestSuiteChromosome> bestSuites = (List<TestSuiteChromosome>) algorithm.getBestIndividuals();
        if (bestSuites.isEmpty()) {
            LoggingUtils.getEvoLogger().warn("Could not find any suitable chromosome");
            return new TestSuiteChromosome();
        } else {
            testSuite = bestSuites.get(0);
        }
    } else {
        zeroFitness.setFinished();
        testSuite = new TestSuiteChromosome();
        for (FitnessFunction<?> ff : testSuite.getFitnessValues().keySet()) {
            testSuite.setCoverage(ff, 1.0);
        }
    }
    long endTime = System.currentTimeMillis() / 1000;
    // Newline after progress bar
    if (Properties.SHOW_PROGRESS)
        LoggingUtils.getEvoLogger().info("");
    String text = " statements, best individual has fitness: ";
    LoggingUtils.getEvoLogger().info("* Search finished after " + (endTime - startTime) + "s and " + algorithm.getAge() + " generations, " + MaxStatementsStoppingCondition.getNumExecutedStatements() + text + testSuite.getFitness());
    // Search is finished, send statistics
    sendExecutionStatistics();
    // We send the info about the total number of coverage goals/targets only after
    // the end of the search. This is because the number of coverage targets may vary
    // when the criterion Properties.Criterion.EXCEPTION is used (exception coverage
    // goal are dynamically added when the generated tests trigger some exceptions
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, algorithm.getFitnessFunctions().size());
    return testSuite;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) ChromosomeFactory(org.evosuite.ga.ChromosomeFactory) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ArrayList(java.util.ArrayList) List(java.util.List) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)1 ChromosomeFactory (org.evosuite.ga.ChromosomeFactory)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 RandomLengthTestFactory (org.evosuite.testcase.factories.RandomLengthTestFactory)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1