Search in sources :

Example 11 with TestSuiteFitnessFunction

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

the class TestSuiteLocalSearchObjective method updateLastFitness.

private void updateLastFitness() {
    lastFitnessSum = 0.0;
    for (TestSuiteFitnessFunction fitness : fitnessFunctions) {
        double newFitness = fitness.getFitness(suite);
        lastFitnessSum += newFitness;
        lastFitness.put(fitness, newFitness);
    }
}
Also used : TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

Example 12 with TestSuiteFitnessFunction

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

the class WholeTestSuiteStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    // Set up search algorithm
    LoggingUtils.getEvoLogger().info("* Setting up search algorithm for whole suite generation");
    PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
    GeneticAlgorithm<TestSuiteChromosome> 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();
    // TODO: Argh, generics.
    algorithm.addFitnessFunctions((List) fitnessFunctions);
    // for(TestSuiteFitnessFunction f : fitnessFunctions)
    // algorithm.addFitnessFunction(f);
    // if (Properties.SHOW_PROGRESS && !logger.isInfoEnabled())
    // FIXME progressMonitor may cause
    algorithm.addListener(progressMonitor);
    if (Properties.TRACK_DIVERSITY)
        algorithm.addListener(new DiversityObserver());
    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);
    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);
        algorithm.generateSolution();
        // TODO: Refactor MOO!
        // bestSuites = (List<TestSuiteChromosome>) ga.getBestIndividuals();
        testSuite = (TestSuiteChromosome) algorithm.getBestIndividual();
    } 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 : DiversityObserver(org.evosuite.testsuite.similarity.DiversityObserver) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

Example 13 with TestSuiteFitnessFunction

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

the class RandomTestStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    LoggingUtils.getEvoLogger().info("* Using random test generation");
    List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    for (TestSuiteFitnessFunction fitnessFunction : fitnessFunctions) suite.addFitness(fitnessFunction);
    List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
    List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>();
    LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
    for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
        goals.addAll(goalFactory.getCoverageGoals());
        LoggingUtils.getEvoLogger().info("  - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
    }
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
    if (!canGenerateTestsForSUT()) {
        LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
        return new TestSuiteChromosome();
    }
    ChromosomeFactory<TestChromosome> factory = getChromosomeFactory();
    StoppingCondition stoppingCondition = getStoppingCondition();
    for (FitnessFunction<?> fitness_function : fitnessFunctions) ((TestSuiteFitnessFunction) fitness_function).getFitness(suite);
    ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
    int number_generations = 0;
    while (!isFinished(suite, stoppingCondition)) {
        number_generations++;
        TestChromosome test = factory.getChromosome();
        TestSuiteChromosome clone = suite.clone();
        clone.addTest(test);
        for (FitnessFunction<?> fitness_function : fitnessFunctions) {
            ((TestSuiteFitnessFunction) fitness_function).getFitness(clone);
            logger.debug("Old fitness: {}, new fitness: {}", suite.getFitness(), clone.getFitness());
        }
        if (clone.compareTo(suite) < 0) {
            suite = clone;
            StatisticsSender.executedAndThenSendIndividualToMaster(clone);
        }
    }
    // statistics.searchFinished(suiteGA);
    LoggingUtils.getEvoLogger().info("* Search Budget:");
    LoggingUtils.getEvoLogger().info("\t- " + stoppingCondition.toString());
    // In the GA, these statistics are sent via the SearchListener when notified about the GA completing
    // Search is finished, send statistics
    sendExecutionStatistics();
    // TODO: Check this: Fitness_Evaluations = getNumExecutedTests?
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Fitness_Evaluations, MaxTestsStoppingCondition.getNumExecutedTests());
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Generations, number_generations);
    return suite;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) MaxTestsStoppingCondition(org.evosuite.ga.stoppingconditions.MaxTestsStoppingCondition) StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 14 with TestSuiteFitnessFunction

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

the class TestGenerationStrategy method getFitnessFunctions.

/**
 * Convert criterion names to test suite fitness functions
 * @return
 */
protected List<TestSuiteFitnessFunction> getFitnessFunctions() {
    List<TestSuiteFitnessFunction> ffs = new ArrayList<TestSuiteFitnessFunction>();
    for (int i = 0; i < Properties.CRITERION.length; i++) {
        TestSuiteFitnessFunction newFunction = FitnessFunctions.getFitnessFunction(Properties.CRITERION[i]);
        // maximization functions
        if (Properties.ALGORITHM != Algorithm.NSGAII && Properties.ALGORITHM != Algorithm.SPEA2) {
            for (TestSuiteFitnessFunction oldFunction : ffs) {
                if (oldFunction.isMaximizationFunction() != newFunction.isMaximizationFunction()) {
                    StringBuffer sb = new StringBuffer();
                    sb.append("* Invalid combination of fitness functions: ");
                    sb.append(oldFunction.toString());
                    if (oldFunction.isMaximizationFunction())
                        sb.append(" is a maximization function ");
                    else
                        sb.append(" is a minimization function ");
                    sb.append(" but ");
                    sb.append(newFunction.toString());
                    if (newFunction.isMaximizationFunction())
                        sb.append(" is a maximization function ");
                    else
                        sb.append(" is a minimization function ");
                    LoggingUtils.getEvoLogger().info(sb.toString());
                    throw new RuntimeException("Invalid combination of fitness functions");
                }
            }
        }
        ffs.add(newFunction);
    }
    return ffs;
}
Also used : ArrayList(java.util.ArrayList) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction)

Example 15 with TestSuiteFitnessFunction

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

the class IndividualTestStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    // Set up search algorithm
    LoggingUtils.getEvoLogger().info("* Setting up search algorithm for individual test generation");
    ExecutionTracer.enableTraceCalls();
    PropertiesTestGAFactory factory = new PropertiesTestGAFactory();
    List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
    long start_time = System.currentTimeMillis() / 1000;
    // Get list of goals
    List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
    // long goalComputationStart = System.currentTimeMillis();
    List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>();
    LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
    for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
        goals.addAll(goalFactory.getCoverageGoals());
        LoggingUtils.getEvoLogger().info("  - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
    }
    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();
    }
    // Need to shuffle goals because the order may make a difference
    if (Properties.SHUFFLE_GOALS) {
        // LoggingUtils.getEvoLogger().info("* Shuffling goals");
        Randomness.shuffle(goals);
    }
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals.size());
    LoggingUtils.getEvoLogger().info("* Total number of test goals: " + goals.size());
    // Bootstrap with random testing to cover easy goals
    // statistics.searchStarted(suiteGA);
    StoppingCondition stoppingCondition = getStoppingCondition();
    // FIXME: just one fitness and one factory?!
    TestSuiteChromosome suite = (TestSuiteChromosome) bootstrapRandomSuite(fitnessFunctions.get(0), goalFactories.get(0));
    Set<Integer> covered = new HashSet<Integer>();
    int covered_goals = 0;
    int num = 0;
    for (TestFitnessFunction fitness_function : goals) {
        if (fitness_function.isCoveredBy(suite)) {
            covered.add(num);
            covered_goals++;
        }
        num++;
    }
    if (covered_goals > 0)
        LoggingUtils.getEvoLogger().info("* Random bootstrapping covered " + covered_goals + " test goals");
    int total_goals = goals.size();
    if (covered_goals == total_goals)
        zeroFitness.setFinished();
    int current_budget = 0;
    long total_budget = Properties.SEARCH_BUDGET;
    LoggingUtils.getEvoLogger().info("* Budget: " + NumberFormat.getIntegerInstance().format(total_budget));
    while (current_budget < total_budget && covered_goals < total_goals && !globalTime.isFinished() && !ShutdownTestWriter.isInterrupted()) {
        long budget = (total_budget - current_budget) / (total_goals - covered_goals);
        logger.info("Budget: " + budget + "/" + (total_budget - current_budget));
        logger.info("Statements: " + current_budget + "/" + total_budget);
        logger.info("Goals covered: " + covered_goals + "/" + total_goals);
        stoppingCondition.setLimit(budget);
        num = 0;
        // //MaxStatementsStoppingCondition.getNumExecutedStatements();
        for (TestFitnessFunction fitnessFunction : goals) {
            if (covered.contains(num)) {
                num++;
                continue;
            }
            GeneticAlgorithm<TestChromosome> ga = factory.getSearchAlgorithm();
            if (Properties.PRINT_CURRENT_GOALS)
                LoggingUtils.getEvoLogger().info("* Searching for goal " + num + ": " + fitnessFunction.toString());
            logger.info("Goal " + num + "/" + (total_goals - covered_goals) + ": " + fitnessFunction);
            if (ShutdownTestWriter.isInterrupted()) {
                num++;
                continue;
            }
            if (globalTime.isFinished()) {
                LoggingUtils.getEvoLogger().info("Skipping goal because time is up");
                num++;
                continue;
            }
            // FitnessFunction fitness_function = new
            ga.addFitnessFunction(fitnessFunction);
            // Perform search
            logger.info("Starting evolution for goal " + fitnessFunction);
            ga.generateSolution();
            if (ga.getBestIndividual().getFitness() == 0.0) {
                if (Properties.PRINT_COVERED_GOALS)
                    // : " +
                    LoggingUtils.getEvoLogger().info("* Covered!");
                // fitness_function.toString());
                logger.info("Found solution, adding to test suite at " + MaxStatementsStoppingCondition.getNumExecutedStatements());
                TestChromosome best = (TestChromosome) ga.getBestIndividual();
                best.getTestCase().addCoveredGoal(fitnessFunction);
                suite.addTest(best);
                // Calculate and keep track of overall fitness
                for (TestSuiteFitnessFunction fitness_function : fitnessFunctions) fitness_function.getFitness(suite);
                covered_goals++;
                covered.add(num);
                // experiment:
                if (Properties.SKIP_COVERED) {
                    Set<Integer> additional_covered_nums = getAdditionallyCoveredGoals(goals, covered, best);
                    // LoggingUtils.getEvoLogger().info("Additionally covered: "+additional_covered_nums.size());
                    for (Integer covered_num : additional_covered_nums) {
                        covered_goals++;
                        covered.add(covered_num);
                    }
                }
            } else {
                logger.info("Found no solution for " + fitnessFunction + " at " + MaxStatementsStoppingCondition.getNumExecutedStatements());
            }
            // statistics.iteration(suiteGA);
            if (Properties.REUSE_BUDGET)
                current_budget += stoppingCondition.getCurrentValue();
            else
                current_budget += budget + 1;
            // print console progress bar
            if (Properties.SHOW_PROGRESS && !(Properties.PRINT_COVERED_GOALS || Properties.PRINT_CURRENT_GOALS)) {
                double percent = current_budget;
                percent = percent / total_budget * 100;
                double coverage = covered_goals;
                coverage = coverage / total_goals * 100;
            // ConsoleProgressBar.printProgressBar((int) percent, (int)
            // coverage);
            }
            if (current_budget > total_budget)
                break;
            num++;
        // break;
        }
    }
    if (Properties.SHOW_PROGRESS)
        LoggingUtils.getEvoLogger().info("");
    // for testing purposes
    if (globalTime.isFinished())
        LoggingUtils.getEvoLogger().info("! Timeout reached");
    if (current_budget >= total_budget)
        LoggingUtils.getEvoLogger().info("! Budget exceeded");
    else
        LoggingUtils.getEvoLogger().info("* Remaining budget: " + (total_budget - current_budget));
    // stoppingCondition.setLimit(Properties.SEARCH_BUDGET);
    // stoppingCondition.forceCurrentValue(current_budget);
    // suiteGA.setStoppingCondition(stopping_condition);
    // suiteGA.addStoppingCondition(global_time);
    // printBudget(suiteGA);
    int c = 0;
    int uncovered_goals = total_goals - covered_goals;
    if (uncovered_goals < 10)
        for (TestFitnessFunction goal : goals) {
            if (!covered.contains(c)) {
                LoggingUtils.getEvoLogger().info("! Unable to cover goal " + c + " " + goal.toString());
            }
            c++;
        }
    else
        LoggingUtils.getEvoLogger().info("! #Goals that were not covered: " + uncovered_goals);
    // statistics.searchFinished(suiteGA);
    long end_time = System.currentTimeMillis() / 1000;
    LoggingUtils.getEvoLogger().info("* Search finished after " + (end_time - start_time) + "s, " + current_budget + " statements, best individual has fitness " + suite.getFitness());
    // Search is finished, send statistics
    sendExecutionStatistics();
    LoggingUtils.getEvoLogger().info("* Covered " + covered_goals + "/" + goals.size() + " goals");
    logger.info("Resulting test suite: " + suite.size() + " tests, length " + suite.totalLengthOfTestCases());
    return suite;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) MaxStatementsStoppingCondition(org.evosuite.ga.stoppingconditions.MaxStatementsStoppingCondition) StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

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