Search in sources :

Example 1 with Criterion

use of org.evosuite.Properties.Criterion in project evosuite by EvoSuite.

the class CoverageCriteriaAnalyzer method analyzeCriteria.

public static void analyzeCriteria(TestSuiteChromosome testSuite, String criteria) {
    // If coverage of target criteria is not already measured
    if (!Properties.COVERAGE) {
        for (Criterion c : Properties.CRITERION) {
            // Analyse coverage for enabled criteria
            // LoggingUtils.getEvoLogger().info("  - " + c.name());
            logger.debug("Measuring coverage of target criterion {}", c);
            analyzeCoverage(testSuite, c.name());
        }
    }
    for (String extraCriterion : Arrays.asList(criteria.toUpperCase().split(","))) {
        if (extraCriterion.equals("CBRANCH")) {
            Properties.INSTRUMENT_METHOD_CALLS = true;
        }
        // Analyse coverage for extra criteria
        if (!ArrayUtil.contains(Properties.CRITERION, extraCriterion)) {
            logger.debug("Measuring additional coverage of target criterion {}", extraCriterion);
            analyzeCoverage(testSuite, extraCriterion);
        }
    }
}
Also used : Criterion(org.evosuite.Properties.Criterion)

Example 2 with Criterion

use of org.evosuite.Properties.Criterion in project evosuite by EvoSuite.

the class CoverageCriteriaAnalyzer method reinstrument.

private static void reinstrument(TestSuiteChromosome testSuite, Properties.Criterion criterion) {
    if (ArrayUtil.contains(Properties.SECONDARY_OBJECTIVE, Properties.SecondaryObjective.IBRANCH)) {
        ExecutionTracer.enableContext();
    }
    if (!ExecutionTracer.isTraceCallsEnabled()) {
        ExecutionTracer.enableTraceCalls();
    }
    testSuite.setChanged(true);
    for (TestChromosome test : testSuite.getTestChromosomes()) {
        test.setChanged(true);
        // clears last execution result and last mutation result
        test.clearCachedResults();
    }
    Properties.Criterion[] oldCriterion = Arrays.copyOf(Properties.CRITERION, Properties.CRITERION.length);
    Properties.CRITERION = new Properties.Criterion[] { criterion };
    logger.info("Re-instrumenting for criterion: " + criterion);
    TestGenerationContext.getInstance().resetContext();
    // Need to load class explicitly in case there are no test cases.
    // If there are tests, then this is redundant
    Properties.getInitializedTargetClass();
    // TODO: Now all existing test cases have reflection objects pointing to the wrong classloader
    logger.info("Changing classloader of test suite for criterion: " + criterion);
    for (TestChromosome test : testSuite.getTestChromosomes()) {
        DefaultTestCase dtest = (DefaultTestCase) test.getTestCase();
        dtest.changeClassLoader(TestGenerationContext.getInstance().getClassLoaderForSUT());
    }
    Properties.CRITERION = oldCriterion;
}
Also used : Criterion(org.evosuite.Properties.Criterion) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 3 with Criterion

use of org.evosuite.Properties.Criterion 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 4 with Criterion

use of org.evosuite.Properties.Criterion in project evosuite by EvoSuite.

the class CoverageAnalysis method printReport.

private static void printReport(List<JUnitResult> results) {
    Iterator<String> it = targetClasses.iterator();
    Criterion[] criterion = Properties.CRITERION;
    while (it.hasNext()) {
        String targetClass = it.next();
        // restart variables
        totalGoals = 0;
        totalCoveredGoals = 0;
        Properties.TARGET_CLASS = targetClass;
        LoggingUtils.getEvoLogger().info("* Target class " + Properties.TARGET_CLASS);
        ClientServices.getInstance().getClientNode().updateProperty("TARGET_CLASS", Properties.TARGET_CLASS);
        for (int criterion_index = 0; criterion_index < criterion.length; criterion_index++) {
            Properties.Criterion c = criterion[criterion_index];
            Properties.CRITERION = new Criterion[] { c };
            analyzeCoverageCriterion(results, c);
        }
        // restore
        Properties.CRITERION = criterion;
        LoggingUtils.getEvoLogger().info("* Total number of covered goals: " + totalCoveredGoals + " / " + totalGoals);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, totalGoals);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Covered_Goals, totalCoveredGoals);
        double coverage = totalGoals == 0 ? 1.0 : ((double) totalCoveredGoals) / ((double) totalGoals);
        LoggingUtils.getEvoLogger().info("* Total coverage: " + NumberFormat.getPercentInstance().format(coverage));
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Coverage, coverage);
        // need to give some time for transmission before client is killed
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // last element will be flush by master process
        if (it.hasNext()) {
            ClientServices.getInstance().getClientNode().flushStatisticsForClassChange();
        }
    }
}
Also used : Criterion(org.evosuite.Properties.Criterion) Criterion(org.evosuite.Properties.Criterion) Properties(org.evosuite.Properties)

Example 5 with Criterion

use of org.evosuite.Properties.Criterion in project evosuite by EvoSuite.

the class CoverageAnalysis method analyzeCoverage.

/**
 * Identify all JUnit tests starting with the given name prefix, instrument
 * and run tests
 */
public static void analyzeCoverage() {
    Sandbox.goingToExecuteSUTCode();
    TestGenerationContext.getInstance().goingToExecuteSUTCode();
    Sandbox.goingToExecuteUnsafeCodeOnSameThread();
    ExecutionTracer.setCheckCallerThread(false);
    try {
        String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
        if (Properties.TARGET_CLASS.endsWith(".jar") || Properties.TARGET_CLASS.contains(File.separator)) {
            targetClasses = DependencyAnalysis.analyzeTarget(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
        } else {
            targetClasses.add(Properties.TARGET_CLASS);
            DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
        }
        LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
    } catch (Throwable e) {
        LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
        logger.error("Problem for " + Properties.TARGET_CLASS + ". Full stack:", e);
        return;
    } finally {
        Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
        Sandbox.doneWithExecutingSUTCode();
        TestGenerationContext.getInstance().doneWithExecutingSUTCode();
    }
    // TestCluster.getInstance();
    List<Class<?>> testClasses = getTestClasses();
    LoggingUtils.getEvoLogger().info("* Found " + testClasses.size() + " test class(es)");
    if (testClasses.isEmpty())
        return;
    /*
         * sort them in a deterministic way, in case there are 
         * static state dependencies
         */
    sortTestClasses(testClasses);
    Class<?>[] tests = testClasses.toArray(new Class<?>[testClasses.size()]);
    LoggingUtils.getEvoLogger().info("* Executing test(s)");
    if (Properties.SELECTED_JUNIT == null) {
        boolean origUseAgent = EvoRunner.useAgent;
        boolean origUseClassLoader = EvoRunner.useClassLoader;
        try {
            // avoid double instrumentation
            EvoRunner.useAgent = false;
            // avoid double instrumentation
            EvoRunner.useClassLoader = false;
            List<JUnitResult> results = executeTests(tests);
            printReport(results);
        } finally {
            EvoRunner.useAgent = origUseAgent;
            EvoRunner.useClassLoader = origUseClassLoader;
        }
    } else {
        // instead of just running junit tests, carve them
        JUnitTestCarvedChromosomeFactory carvedFactory = new JUnitTestCarvedChromosomeFactory(null);
        TestSuiteChromosome testSuite = carvedFactory.getCarvedTestSuite();
        int goals = 0;
        for (Properties.Criterion pc : Properties.CRITERION) {
            LoggingUtils.getEvoLogger().info("* Coverage analysis for criterion " + pc);
            TestFitnessFactory ffactory = FitnessFunctions.getFitnessFactory(pc);
            goals += ffactory.getCoverageGoals().size();
            FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(pc);
            ffunction.getFitness(testSuite);
            CoverageCriteriaAnalyzer.analyzeCoverage(testSuite, pc);
        }
        // Generate test suite
        TestSuiteGenerator.writeJUnitTestsAndCreateResult(testSuite);
        StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals);
        if (Properties.COVERAGE_MATRIX)
            throw new IllegalArgumentException("Coverage matrix not yet available when measuring coverage of a carved test suite");
    }
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) Properties(org.evosuite.Properties) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestClass(org.junit.runners.model.TestClass) Criterion(org.evosuite.Properties.Criterion)

Aggregations

Criterion (org.evosuite.Properties.Criterion)7 Properties (org.evosuite.Properties)6 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)2 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)2 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1 RegressionTestSuiteChromosome (org.evosuite.regression.RegressionTestSuiteChromosome)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestCase (org.evosuite.testcase.TestCase)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 JUnitTestCarvedChromosomeFactory (org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory)1 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)1 TestClass (org.junit.runners.model.TestClass)1