Search in sources :

Example 1 with TestChromosome

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

the class SearchStatistics method minimized.

/**
 * {@inheritDoc}
 */
@Override
public void minimized(Chromosome chromosome) {
    TestSuiteChromosome best = (TestSuiteChromosome) chromosome;
    StatisticEntry entry = statistics.get(statistics.size() - 1);
    entry.tests = best.getTests();
    // TODO: Remember which lines were covered
    // This information is in ExecutionTrace.coverage
    entry.size_minimized = best.size();
    entry.length_minimized = best.totalLengthOfTestCases();
    entry.minimized_time = System.currentTimeMillis();
    entry.coverage = new HashSet<Integer>();
    entry.coveredIntraMethodPairs = 0;
    entry.coveredInterMethodPairs = 0;
    entry.coveredIntraClassPairs = 0;
    entry.coveredParameterPairs = 0;
    entry.aliasingIntraMethodPairs = 0;
    entry.aliasingInterMethodPairs = 0;
    entry.aliasingIntraClassPairs = 0;
    entry.aliasingParameterPairs = 0;
    entry.coveredAliasIntraMethodPairs = 0;
    entry.coveredAliasInterMethodPairs = 0;
    entry.coveredAliasIntraClassPairs = 0;
    entry.coveredAliasParameterPairs = 0;
    // TODO isn't this more or less copy-paste of
    // BranchCoverageSuiteFitness.getFitness()?
    // DONE To make this work for other criteria too, it would be perfect if
    // one
    // could ask every suite fitness how many goals were covered
    logger.debug("Calculating coverage of best individual with fitness " + chromosome.getFitness());
    Map<Integer, Double> true_distance = new HashMap<Integer, Double>();
    Map<Integer, Double> false_distance = new HashMap<Integer, Double>();
    Map<Integer, Integer> predicate_count = new HashMap<Integer, Integer>();
    Set<String> covered_methods = new HashSet<String>();
    Map<String, Set<Class<?>>> implicitTypesOfExceptions = new HashMap<String, Set<Class<?>>>();
    Map<String, Set<Class<?>>> explicitTypesOfExceptions = new HashMap<String, Set<Class<?>>>();
    Map<TestCase, Map<Integer, Boolean>> isExceptionExplicit = new HashMap<TestCase, Map<Integer, Boolean>>();
    Set<DefUseCoverageTestFitness> coveredDUGoals = new HashSet<DefUseCoverageTestFitness>();
    if (ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.DEFUSE) || Properties.ANALYSIS_CRITERIA.toUpperCase().contains("DEFUSE")) {
        for (DefUseCoverageTestFitness goal : DefUseCoverageFactory.getDUGoals()) {
            if (goal.isInterMethodPair())
                entry.numInterMethodPairs++;
            else if (goal.isIntraClassPair())
                entry.numIntraClassPairs++;
            else if (goal.isParameterGoal())
                entry.numParameterPairs++;
            else
                entry.numIntraMethodPairs++;
            if (goal.isAlias()) {
                if (goal.isInterMethodPair())
                    entry.aliasingInterMethodPairs++;
                else if (goal.isIntraClassPair())
                    entry.aliasingIntraClassPairs++;
                else if (goal.isParameterGoal())
                    entry.aliasingParameterPairs++;
                else
                    entry.aliasingIntraMethodPairs++;
            }
        }
        entry.numDefinitions = DefUsePool.getDefCounter();
        entry.numUses = DefUsePool.getUseCounter();
        entry.numDefUsePairs = DefUseCoverageFactory.getDUGoals().size();
    }
    logger.debug("Calculating line coverage");
    for (TestChromosome test : best.tests) {
        ExecutionResult result = executeTest(test, entry.className);
        ExecutionTrace trace = result.getTrace();
        entry.coverage.addAll(getCoveredLines(trace, entry.className));
        isExceptionExplicit.put(test.getTestCase(), result.explicitExceptions);
        if (ArrayUtil.contains(Properties.CRITERION, Properties.Criterion.DEFUSE) || Properties.ANALYSIS_CRITERIA.toUpperCase().contains("DEFUSE")) {
            for (DefUseCoverageTestFitness goal : DefUseCoverageFactory.getDUGoals()) {
                if (coveredDUGoals.contains(goal))
                    continue;
                if (goal.isCovered(result)) {
                    coveredDUGoals.add(goal);
                    if (goal.isInterMethodPair()) {
                        entry.coveredInterMethodPairs++;
                        if (goal.isAlias()) {
                            entry.coveredAliasInterMethodPairs++;
                        }
                    } else if (goal.isIntraClassPair()) {
                        entry.coveredIntraClassPairs++;
                        if (goal.isAlias()) {
                            entry.coveredAliasIntraClassPairs++;
                        }
                    } else if (goal.isParameterGoal()) {
                        entry.coveredParameterPairs++;
                        if (goal.isAlias()) {
                            entry.coveredAliasParameterPairs++;
                        }
                    } else {
                        entry.coveredIntraMethodPairs++;
                        if (goal.isAlias()) {
                            entry.coveredAliasIntraMethodPairs++;
                        }
                    }
                }
            }
        }
        for (String method : trace.getCoveredMethods()) {
            if (method.startsWith(Properties.TARGET_CLASS) || method.startsWith(Properties.TARGET_CLASS + '$'))
                covered_methods.add(method);
        }
        for (Entry<Integer, Double> e : trace.getTrueDistances().entrySet()) {
            if (!predicate_count.containsKey(e.getKey()))
                predicate_count.put(e.getKey(), 1);
            else
                predicate_count.put(e.getKey(), predicate_count.get(e.getKey()) + 1);
            if (!true_distance.containsKey(e.getKey()) || true_distance.get(e.getKey()) > e.getValue()) {
                true_distance.put(e.getKey(), e.getValue());
            }
        }
        for (Entry<Integer, Double> e : trace.getFalseDistances().entrySet()) {
            if (!predicate_count.containsKey(e.getKey()))
                predicate_count.put(e.getKey(), 1);
            else
                predicate_count.put(e.getKey(), predicate_count.get(e.getKey()) + 1);
            if (!false_distance.containsKey(e.getKey()) || false_distance.get(e.getKey()) > e.getValue()) {
                false_distance.put(e.getKey(), e.getValue());
            }
        }
    }
    for (TestCase test : entry.results.keySet()) {
        Map<Integer, Throwable> exceptions = entry.results.get(test);
        // iterate on the indexes of the statements that resulted in an exception
        for (Integer i : exceptions.keySet()) {
            Throwable t = exceptions.get(i);
            if (t instanceof SecurityException && Properties.SANDBOX)
                continue;
            if (i >= test.size()) {
                // Timeouts are put after the last statement if the process was forcefully killed
                continue;
            }
            String methodName = "";
            boolean sutException = false;
            if (test.getStatement(i) instanceof MethodStatement) {
                MethodStatement ms = (MethodStatement) test.getStatement(i);
                Method method = ms.getMethod().getMethod();
                methodName = method.getName() + Type.getMethodDescriptor(method);
                if (method.getDeclaringClass().equals(Properties.getTargetClass()))
                    sutException = true;
            } else if (test.getStatement(i) instanceof ConstructorStatement) {
                ConstructorStatement cs = (ConstructorStatement) test.getStatement(i);
                Constructor<?> constructor = cs.getConstructor().getConstructor();
                methodName = "<init>" + Type.getConstructorDescriptor(constructor);
                if (constructor.getDeclaringClass().equals(Properties.getTargetClass()))
                    sutException = true;
            }
            boolean notDeclared = !test.getStatement(i).getDeclaredExceptions().contains(t.getClass());
            if (notDeclared && sutException) {
                /*
					 * we need to distinguish whether it is explicit (ie "throw" in the code, eg for validating
					 * input for pre-condition) or implicit ("likely" a real fault).
					 */
                /*
					 * FIXME: need to find a way to calculate it
					 */
                boolean isExplicit = isExceptionExplicit.get(test).containsKey(i) && isExceptionExplicit.get(test).get(i);
                if (isExplicit) {
                    if (!explicitTypesOfExceptions.containsKey(methodName))
                        explicitTypesOfExceptions.put(methodName, new HashSet<Class<?>>());
                    explicitTypesOfExceptions.get(methodName).add(t.getClass());
                } else {
                    if (!implicitTypesOfExceptions.containsKey(methodName))
                        implicitTypesOfExceptions.put(methodName, new HashSet<Class<?>>());
                    implicitTypesOfExceptions.get(methodName).add(t.getClass());
                }
            }
        }
    }
    int num_covered = 0;
    entry.error_branches = BranchPool.getNumArtificialBranches();
    for (Integer key : predicate_count.keySet()) {
        // logger.info("Key: "+key);
        double df = true_distance.get(key);
        double dt = false_distance.get(key);
        Branch b = BranchPool.getBranch(key);
        if (!b.getClassName().startsWith(Properties.TARGET_CLASS) && !b.getClassName().startsWith(Properties.TARGET_CLASS + '$'))
            continue;
        // if (!b.isInstrumented()) {
        if (df == 0.0)
            num_covered++;
        if (dt == 0.0)
            num_covered++;
        // }
        if (b.isInstrumented()) {
            // entry.error_branches++;
            if (df == 0.0)
                entry.error_branches_covered++;
            if (dt == 0.0)
                entry.error_branches_covered++;
        }
    }
    for (String methodName : CFGMethodAdapter.getMethodsPrefix(Properties.TARGET_CLASS)) {
        boolean allArtificial = true;
        int splitPoint = methodName.lastIndexOf(".");
        String cName = methodName.substring(0, splitPoint);
        String mName = methodName.substring(splitPoint + 1);
        boolean hasBranches = false;
        for (Branch b : BranchPool.retrieveBranchesInMethod(cName, mName)) {
            hasBranches = true;
            if (!b.isInstrumented()) {
                allArtificial = false;
                break;
            }
        }
        if (hasBranches && allArtificial) {
            entry.error_branchless_methods++;
            if (covered_methods.contains(methodName)) {
                entry.error_branchless_methods_covered++;
            }
        }
    }
    int coveredBranchlessMethods = 0;
    for (String branchlessMethod : BranchPool.getBranchlessMethodsMemberClasses(Properties.TARGET_CLASS)) {
        if (covered_methods.contains(branchlessMethod))
            coveredBranchlessMethods++;
    }
    // + covered branchless methods?
    entry.covered_branches = num_covered;
    entry.covered_methods = covered_methods.size();
    entry.covered_branchless_methods = coveredBranchlessMethods;
    // BranchCoverageSuiteFitness f = new BranchCoverageSuiteFitness();
    /*
		if (Properties.CRITERION == Properties.Criterion.DEFUSE
		        || Properties.ANALYSIS_CRITERIA.contains("DefUse")) {
			entry.coveredIntraMethodPairs = DefUseCoverageSuiteFitness.mostCoveredGoals.get(DefUsePairType.INTRA_METHOD);
			entry.coveredInterMethodPairs = DefUseCoverageSuiteFitness.mostCoveredGoals.get(DefUsePairType.INTER_METHOD);
			entry.coveredIntraClassPairs = DefUseCoverageSuiteFitness.mostCoveredGoals.get(DefUsePairType.INTRA_CLASS);
			entry.coveredParameterPairs = DefUseCoverageSuiteFitness.mostCoveredGoals.get(DefUsePairType.PARAMETER);
		}
			*/
    // System.out.println(covered_methods);
    // DONE make this work for other criteria too. this will only work for
    // branch coverage - see searchStarted()/Finished()
    // entry.total_goals = 2 * entry.total_branches +
    // entry.branchless_methods; - moved to searchStarted()
    // entry.covered_goals = num_covered; - moved to searchFinished()
    // for(String e : CFGMethodAdapter.branchless_methods) {
    // for (String e : CFGMethodAdapter.methods) {
    // for (String e : BranchPool.getBranchlessMethods()) {
    // if (covered_methods.contains(e)) {
    // logger.info("Covered method: " + e);
    // entry.covered_goals++;
    // } else {
    // logger.info("Method is not covered: " + e);
    // }
    /*
		 * logger.debug("Covered methods: " + covered_methods.size() + "/" +
		 * entry.total_methods); for (String method : covered_methods) {
		 * logger.debug("Covered method: " + method); }
		 */
    // }
    String s = calculateCoveredBranchesBitString(best);
    entry.goalCoverage = s;
    entry.explicitMethodExceptions = getNumExceptions(explicitTypesOfExceptions);
    entry.explicitTypeExceptions = getNumClassExceptions(explicitTypesOfExceptions);
    entry.implicitMethodExceptions = getNumExceptions(implicitTypesOfExceptions);
    entry.implicitTypeExceptions = getNumClassExceptions(implicitTypesOfExceptions);
    entry.implicitExceptions = implicitTypesOfExceptions;
    entry.explicitExceptions = explicitTypesOfExceptions;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) DefUseCoverageTestFitness(org.evosuite.coverage.dataflow.DefUseCoverageTestFitness) Branch(org.evosuite.coverage.branch.Branch) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet) ConstructorStatement(org.evosuite.testcase.ConstructorStatement) MethodStatement(org.evosuite.testcase.MethodStatement) Constructor(java.lang.reflect.Constructor) ExecutionTrace(org.evosuite.testcase.ExecutionTrace) ExecutionResult(org.evosuite.testcase.ExecutionResult) Method(java.lang.reflect.Method) TestCase(org.evosuite.testcase.TestCase) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with TestChromosome

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

the class PrimePathSuiteFitness method getFitness.

/* (non-Javadoc)
	 * @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
    List<ExecutionResult> results = runTestSuite(suite);
    List<TestFitnessFunction> coveredGoals = new ArrayList<TestFitnessFunction>();
    double fitness = 0.0;
    for (TestFitnessFunction goal : goals) {
        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;
    }
    suite.setCoverage(this, coveredGoals.size() / (double) goals.size());
    updateIndividual(this, suite, fitness);
    return fitness;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) ExecutionResult(org.evosuite.testcase.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 3 with TestChromosome

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

the class TestCaseReplacer method replaceTest.

/**
 * Given a test, create a GA and look for a replacement test
 *
 * @param test
 */
public TestCase replaceTest(String targetClass, List<TestCase> otherTests, TestCase test) {
    // Various environmental setup necessary for EvoSuite
    Properties.ALGORITHM = Algorithm.MONOTONICGA;
    Properties.STRATEGY = Strategy.ONEBRANCH;
    ExecutionTracer.enableTraceCalls();
    // Run for 10 generations - adapt as necessary
    // Properties.STOPPING_CONDITION = StoppingCondition.MAXGENERATIONS;
    // Properties.SEARCH_BUDGET = 20;
    // Properties.STOPPING_CONDITION = StoppingCondition.MAXTIME;
    // Properties.SEARCH_BUDGET = 20;
    // GeneticAlgorithm ga = TestSuiteGenerator.getGeneticAlgorithm(new RandomLengthTestFactory());
    // TODO: JM: Needs Testing. Not sure if this is equivalent:
    PropertiesTestGAFactory algorithmFactory = new PropertiesTestGAFactory();
    GeneticAlgorithm<TestChromosome> ga = algorithmFactory.getSearchAlgorithm();
    List<TestFitnessFactory<? extends TestFitnessFunction>> factories = TestSuiteGenerator.getFitnessFactories();
    Collection<TestFitnessFunction> fitnessFunctions = new ArrayList<TestFitnessFunction>();
    for (TestFitnessFactory<? extends TestFitnessFunction> factory : factories) {
        // Set up fitness function for the parsed test case
        DifferenceFitnessFunction fitnessFunction = new DifferenceFitnessFunction(test, otherTests, factory);
        // ga.setFitnessFunction(fitness);
        fitnessFunctions.add(fitnessFunction);
        ga.addFitnessFunction(fitnessFunction);
    }
    // Perform calculation
    ga.generateSolution();
    // The best individual at the end of the search contains our candidate solution
    TestChromosome testChromosome = (TestChromosome) ga.getBestIndividual();
    TestCaseMinimizer minimizer = new TestCaseMinimizer(fitnessFunctions);
    minimizer.minimize(testChromosome);
    System.out.println("Best individual has fitness: " + testChromosome.getFitness());
    return testChromosome.getTestCase();
}
Also used : TestCaseMinimizer(org.evosuite.testcase.TestCaseMinimizer) PropertiesTestGAFactory(org.evosuite.strategy.PropertiesTestGAFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 4 with TestChromosome

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

the class TestSuiteGenerator method postProcessTests.

/**
 * Apply any readability optimizations and other techniques that should use
 * or modify the generated tests
 *
 * @param testSuite
 */
protected void postProcessTests(TestSuiteChromosome testSuite) {
    // If overall time is short, the search might not have had enough time
    // to come up with a suite without timeouts. However, they will slow
    // down
    // the rest of the process, and may lead to invalid tests
    testSuite.getTestChromosomes().removeIf(t -> t.getLastExecutionResult() != null && (t.getLastExecutionResult().hasTimeout() || t.getLastExecutionResult().hasTestException()));
    if (Properties.CTG_SEEDS_FILE_OUT != null) {
        TestSuiteSerialization.saveTests(testSuite, new File(Properties.CTG_SEEDS_FILE_OUT));
    } else if (Properties.TEST_FACTORY == TestFactory.SERIALIZATION) {
        TestSuiteSerialization.saveTests(testSuite, new File(Properties.SEED_DIR + File.separator + Properties.TARGET_CLASS));
    }
    /*
		 * Remove covered goals that are not part of the minimization targets,
		 * as they might screw up coverage analysis when a minimization timeout
		 * occurs. This may happen e.g. when MutationSuiteFitness calls
		 * BranchCoverageSuiteFitness which adds branch goals.
		 */
    // TODO: This creates an inconsistency between
    // suite.getCoveredGoals().size() and suite.getNumCoveredGoals()
    // but it is not clear how to update numcoveredgoals
    List<TestFitnessFunction> goals = new ArrayList<>();
    for (TestFitnessFactory<?> ff : getFitnessFactories()) {
        goals.addAll(ff.getCoverageGoals());
    }
    for (TestFitnessFunction f : testSuite.getCoveredGoals()) {
        if (!goals.contains(f)) {
            testSuite.removeCoveredGoal(f);
        }
    }
    if (Properties.INLINE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.INLINING);
        ConstantInliner inliner = new ConstantInliner();
        // progressMonitor.setCurrentPhase("Inlining constants");
        // Map<FitnessFunction<? extends TestSuite<?>>, Double> fitnesses =
        // testSuite.getFitnesses();
        inliner.inline(testSuite);
    }
    if (Properties.MINIMIZE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.MINIMIZATION);
        // progressMonitor.setCurrentPhase("Minimizing test cases");
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping minimization because not enough time is left");
            ClientServices.track(RuntimeVariable.Result_Size, testSuite.size());
            ClientServices.track(RuntimeVariable.Minimized_Size, testSuite.size());
            ClientServices.track(RuntimeVariable.Result_Length, testSuite.totalLengthOfTestCases());
            ClientServices.track(RuntimeVariable.Minimized_Length, testSuite.totalLengthOfTestCases());
        } else if (Properties.isRegression()) {
            RegressionSuiteMinimizer minimizer = new RegressionSuiteMinimizer();
            minimizer.minimize(testSuite);
        } else {
            double before = testSuite.getFitness();
            TestSuiteMinimizer minimizer = new TestSuiteMinimizer(getFitnessFactories());
            LoggingUtils.getEvoLogger().info("* Minimizing test suite");
            minimizer.minimize(testSuite, true);
            double after = testSuite.getFitness();
            if (after > before + 0.01d) {
                // assume minimization
                throw new Error("EvoSuite bug: minimization lead fitness from " + before + " to " + after);
            }
        }
    } else {
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping minimization because not enough time is left");
        }
        ClientServices.track(RuntimeVariable.Result_Size, testSuite.size());
        ClientServices.track(RuntimeVariable.Minimized_Size, testSuite.size());
        ClientServices.track(RuntimeVariable.Result_Length, testSuite.totalLengthOfTestCases());
        ClientServices.track(RuntimeVariable.Minimized_Length, testSuite.totalLengthOfTestCases());
    }
    if (Properties.COVERAGE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.COVERAGE_ANALYSIS);
        CoverageCriteriaAnalyzer.analyzeCoverage(testSuite);
    }
    double coverage = testSuite.getCoverage();
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
    // SearchStatistics.getInstance().mutationScore(coverage);
    }
    StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
    LoggingUtils.getEvoLogger().info("* Generated " + testSuite.size() + " tests with total length " + testSuite.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(testSuite, Properties.ANALYSIS_CRITERIA);
    // FIXME: can we send all bestSuites?
    }
    if (Properties.CRITERION.length > 1)
        LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage) + " (average coverage for all fitness functions)");
    else
        LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage));
    // printBudget(ga); // TODO - need to move this somewhere else
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) && Properties.ANALYSIS_CRITERIA.isEmpty())
        DefUseCoverageSuiteFitness.printCoverage();
    DSEStats.getInstance().trackConstraintTypes();
    DSEStats.getInstance().trackSolverStatistics();
    if (Properties.DSE_PROBABILITY > 0.0 && Properties.LOCAL_SEARCH_RATE > 0 && Properties.LOCAL_SEARCH_PROBABILITY > 0.0) {
        DSEStats.getInstance().logStatistics();
    }
    if (Properties.FILTER_SANDBOX_TESTS) {
        for (TestChromosome test : testSuite.getTestChromosomes()) {
            // delete all statements leading to security exceptions
            ExecutionResult result = test.getLastExecutionResult();
            if (result == null) {
                result = TestCaseExecutor.runTest(test.getTestCase());
            }
            if (result.hasSecurityException()) {
                int position = result.getFirstPositionOfThrownException();
                if (position > 0) {
                    test.getTestCase().chop(position);
                    result = TestCaseExecutor.runTest(test.getTestCase());
                    test.setLastExecutionResult(result);
                }
            }
        }
    }
    if (Properties.ASSERTIONS && !Properties.isRegression()) {
        LoggingUtils.getEvoLogger().info("* Generating assertions");
        // progressMonitor.setCurrentPhase("Generating assertions");
        ClientServices.getInstance().getClientNode().changeState(ClientState.ASSERTION_GENERATION);
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping assertion generation because not enough time is left");
        } else {
            TestSuiteGeneratorHelper.addAssertions(testSuite);
        }
        // FIXME: can we
        StatisticsSender.sendIndividualToMaster(testSuite);
    // pass the list
    // of
    // testsuitechromosomes?
    }
    if (Properties.NO_RUNTIME_DEPENDENCY) {
        LoggingUtils.getEvoLogger().info("* Property NO_RUNTIME_DEPENDENCY is set to true - skipping JUnit compile check");
        LoggingUtils.getEvoLogger().info("* WARNING: Not including the runtime dependencies is likely to lead to flaky tests!");
    } else if (Properties.JUNIT_TESTS && Properties.JUNIT_CHECK) {
        compileAndCheckTests(testSuite);
    }
    if (Properties.SERIALIZE_REGRESSION_TEST_SUITE) {
        RegressionSuiteSerializer.appendToRegressionTestSuite(testSuite);
    }
    if (Properties.isRegression() && Properties.KEEP_REGRESSION_ARCHIVE) {
        RegressionSuiteSerializer.storeRegressionArchive();
    }
}
Also used : ConstantInliner(org.evosuite.testcase.ConstantInliner) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RegressionSuiteMinimizer(org.evosuite.regression.RegressionSuiteMinimizer) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) File(java.io.File) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 5 with TestChromosome

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

the class OnlyBranchCoverageSuiteFitness method analyzeTraces.

/**
 * Iterate over all execution results and summarize statistics
 *
 * @param results
 * @param predicateCount
 * @param callCount
 * @param trueDistance
 * @param falseDistance
 * @return
 */
private boolean analyzeTraces(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite, List<ExecutionResult> results, Map<Integer, Integer> predicateCount, Map<Integer, Double> trueDistance, Map<Integer, Double> falseDistance) {
    boolean hasTimeoutOrTestException = false;
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            hasTimeoutOrTestException = true;
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        for (Entry<Integer, Integer> entry : result.getTrace().getPredicateExecutionCount().entrySet()) {
            if (!branchesId.contains(entry.getKey()) || (removedBranchesT.contains(entry.getKey()) && removedBranchesF.contains(entry.getKey())))
                continue;
            if (!predicateCount.containsKey(entry.getKey()))
                predicateCount.put(entry.getKey(), entry.getValue());
            else {
                predicateCount.put(entry.getKey(), predicateCount.get(entry.getKey()) + entry.getValue());
            }
        }
        for (Entry<Integer, Double> entry : result.getTrace().getTrueDistances().entrySet()) {
            if (!branchesId.contains(entry.getKey()) || removedBranchesT.contains(entry.getKey()))
                continue;
            if (!trueDistance.containsKey(entry.getKey()))
                trueDistance.put(entry.getKey(), entry.getValue());
            else {
                trueDistance.put(entry.getKey(), Math.min(trueDistance.get(entry.getKey()), entry.getValue()));
            }
            OnlyBranchCoverageTestFitness goal = (OnlyBranchCoverageTestFitness) branchCoverageTrueMap.get(entry.getKey());
            if ((Double.compare(entry.getValue(), 0.0) == 0)) {
                test.getTestCase().addCoveredGoal(goal);
                toRemoveBranchesT.add(entry.getKey());
            }
            if (Properties.TEST_ARCHIVE) {
                Archive.getArchiveInstance().updateArchive(goal, test, entry.getValue());
            }
        }
        for (Entry<Integer, Double> entry : result.getTrace().getFalseDistances().entrySet()) {
            if (!branchesId.contains(entry.getKey()) || removedBranchesF.contains(entry.getKey()))
                continue;
            if (!falseDistance.containsKey(entry.getKey()))
                falseDistance.put(entry.getKey(), entry.getValue());
            else {
                falseDistance.put(entry.getKey(), Math.min(falseDistance.get(entry.getKey()), entry.getValue()));
            }
            OnlyBranchCoverageTestFitness goal = (OnlyBranchCoverageTestFitness) branchCoverageFalseMap.get(entry.getKey());
            if ((Double.compare(entry.getValue(), 0.0) == 0)) {
                test.getTestCase().addCoveredGoal(goal);
                toRemoveBranchesF.add(entry.getKey());
            }
            if (Properties.TEST_ARCHIVE) {
                Archive.getArchiveInstance().updateArchive(goal, test, entry.getValue());
            }
        }
    }
    return hasTimeoutOrTestException;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

TestChromosome (org.evosuite.testcase.TestChromosome)128 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)47 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)33 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)22 ArrayList (java.util.ArrayList)17 TestCase (org.evosuite.testcase.TestCase)17 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)16 HashSet (java.util.HashSet)15 Properties (org.evosuite.Properties)15 Test (org.junit.Test)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)14 HashMap (java.util.HashMap)11 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)10 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)8 LinkedHashMap (java.util.LinkedHashMap)7 Foo (com.examples.with.different.packagename.symbolic.Foo)6 LinkedHashSet (java.util.LinkedHashSet)6 Set (java.util.Set)6 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 Map (java.util.Map)5