Search in sources :

Example 11 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class StatementCoverageSuiteFitness method getFitness.

/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
    List<ExecutionResult> results = runTestSuite(suite);
    double fitness = 0.0;
    // first simple and naive idea:
    // just take each goal, calculate the minimal fitness over all results in the suite
    // once a goal is covered don't check for it again
    // in the end sum up all those fitness and it's the resulting suite-fitness
    // guess this is horribly inefficient but it's a start
    List<? extends TestFitnessFunction> totalGoals = StatementCoverageFactory.retrieveCoverageGoals();
    Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>();
    for (TestFitnessFunction goal : totalGoals) {
        double goalFitness = Double.MAX_VALUE;
        for (ExecutionResult result : results) {
            TestChromosome tc = new TestChromosome();
            tc.setTestCase(result.test);
            double resultFitness = goal.getFitness(tc, result);
            if (resultFitness < goalFitness)
                goalFitness = resultFitness;
            if (goalFitness == 0.0) {
                // result.test.addCoveredGoal(goal);
                coveredGoals.add(goal);
                break;
            }
        }
        fitness += goalFitness;
    }
    if (totalGoals.size() > 0)
        suite.setCoverage(this, coveredGoals.size() / (double) totalGoals.size());
    else
        suite.setCoverage(this, 1.0);
    suite.setNumOfCoveredGoals(this, coveredGoals.size());
    updateIndividual(this, suite, fitness);
    return fitness;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Example 12 with TestFitnessFunction

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

Example 13 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class DSEStrategy method getGoals.

private List<TestFitnessFunction> getGoals(boolean verbose) {
    List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
    List<TestFitnessFunction> goals = new ArrayList<>();
    if (goalFactories.size() == 1) {
        TestFitnessFactory<? extends TestFitnessFunction> factory = goalFactories.iterator().next();
        goals.addAll(factory.getCoverageGoals());
        if (verbose) {
            LoggingUtils.getEvoLogger().info("* Total number of test goals: {}", factory.getCoverageGoals().size());
            if (Properties.PRINT_GOALS) {
                for (TestFitnessFunction goal : factory.getCoverageGoals()) LoggingUtils.getEvoLogger().info("" + goal.toString());
            }
        }
    } else {
        if (verbose) {
            LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
        }
        for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
            goals.addAll(goalFactory.getCoverageGoals());
            if (verbose) {
                LoggingUtils.getEvoLogger().info("  - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
                if (Properties.PRINT_GOALS) {
                    for (TestFitnessFunction goal : goalFactory.getCoverageGoals()) LoggingUtils.getEvoLogger().info("" + goal.toString());
                }
            }
        }
    }
    return goals;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory)

Example 14 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction 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;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) Properties(org.evosuite.Properties) TestCase(org.evosuite.testcase.TestCase) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Criterion(org.evosuite.Properties.Criterion)

Example 15 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction 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

TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)73 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)26 TestCase (org.evosuite.testcase.TestCase)24 Test (org.junit.Test)22 TestChromosome (org.evosuite.testcase.TestChromosome)21 ArrayList (java.util.ArrayList)20 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)11 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)8 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)8 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)8 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)4 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)4 Properties (org.evosuite.Properties)3