Search in sources :

Example 36 with TestChromosome

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

the class MIO method generateSolution.

/**
 * {@inheritDoc}
 */
@Override
public void generateSolution() {
    if (this.population.isEmpty()) {
        this.initializePopulation();
        assert !this.population.isEmpty() : "Initial population is empty, i.e., EvoSuite could not create any test!";
    }
    if (Properties.ENABLE_SECONDARY_OBJECTIVE_AFTER > 0 || Properties.ENABLE_SECONDARY_OBJECTIVE_STARVATION) {
        this.disableFirstSecondaryCriterion();
    }
    logger.debug("Starting evolution");
    while (!this.isFinished()) {
        this.evolve();
        if (this.shouldApplyLocalSearch()) {
            // solutions in the archive
            if (Archive.getArchiveInstance().hasBeenUpdated()) {
                Set<TestChromosome> testsInArchive = Archive.getArchiveInstance().getSolutions();
                if (!testsInArchive.isEmpty()) {
                    TestSuiteChromosome individualInPopulation = ((TestSuiteChromosome) this.population.get(0));
                    individualInPopulation.clearTests();
                    for (TestChromosome test : testsInArchive) {
                        individualInPopulation.addTest(test.getTestCase().clone());
                    }
                }
            }
            this.applyLocalSearch();
        }
        logger.info("Updating fitness values");
        this.updateFitnessFunctionsAndValues();
        logger.info("Current iteration: " + currentIteration);
        this.notifyIteration();
    }
    TimeController.execute(this::updateBestIndividualFromArchive, "Update from archive", 5_000);
    this.notifySearchFinished();
}
Also used : TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 37 with TestChromosome

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

the class CoverageAnalysis method analyzeCoverageCriterion.

private static void analyzeCoverageCriterion(List<JUnitResult> results, Properties.Criterion criterion) {
    logger.info("analysing coverage of " + criterion);
    // Factory
    TestFitnessFactory<? extends TestFitnessFunction> factory = FitnessFunctions.getFitnessFactory(criterion);
    // Goals
    List<?> goals = null;
    if (criterion == Criterion.MUTATION || criterion == Criterion.STRONGMUTATION) {
        goals = MutationPool.getMutants();
    } else {
        goals = factory.getCoverageGoals();
    }
    totalGoals += goals.size();
    // A dummy Chromosome
    TestChromosome dummy = new TestChromosome();
    dummy.setChanged(false);
    // Execution result of a dummy Test Case
    ExecutionResult executionResult = new ExecutionResult(dummy.getTestCase());
    // coverage matrix (each row represents the coverage of each test case
    // and each column represents the coverage of each component (e.g., line)
    // this coverage matrix is useful for Rho fitness
    // +1 because we also want to include the test result
    boolean[][] coverage_matrix = new boolean[results.size()][goals.size() + 1];
    BitSet covered = new BitSet(goals.size());
    for (int index_test = 0; index_test < results.size(); index_test++) {
        JUnitResult tR = results.get(index_test);
        ExecutionTrace trace = tR.getExecutionTrace();
        executionResult.setTrace(trace);
        dummy.getTestCase().clearCoveredGoals();
        dummy.setLastExecutionResult(executionResult);
        if (criterion == Criterion.MUTATION || criterion == Criterion.STRONGMUTATION) {
            for (Integer mutationID : trace.getTouchedMutants()) {
                Mutation mutation = MutationPool.getMutant(mutationID);
                if (goals.contains(mutation)) {
                    MutationObserver.activateMutation(mutationID);
                    List<JUnitResult> mutationResults = executeTests(tR.getJUnitClass());
                    MutationObserver.deactivateMutation();
                    for (JUnitResult mR : mutationResults) {
                        if (mR.getFailureCount() != tR.getFailureCount()) {
                            logger.info("Mutation killed: " + mutationID);
                            covered.set(mutation.getId());
                            coverage_matrix[index_test][mutationID.intValue()] = true;
                            break;
                        }
                    }
                }
            }
        } else {
            for (int index_component = 0; index_component < goals.size(); index_component++) {
                TestFitnessFunction goal = (TestFitnessFunction) goals.get(index_component);
                if (goal.isCovered(dummy)) {
                    covered.set(index_component);
                    coverage_matrix[index_test][index_component] = true;
                } else {
                    coverage_matrix[index_test][index_component] = false;
                }
            }
        }
        coverage_matrix[index_test][goals.size()] = tR.wasSuccessful();
    }
    totalCoveredGoals += covered.cardinality();
    if (Properties.COVERAGE_MATRIX) {
        CoverageReportGenerator.writeCoverage(coverage_matrix, criterion);
    }
    StringBuilder str = new StringBuilder();
    for (int index_component = 0; index_component < goals.size(); index_component++) {
        str.append(covered.get(index_component) ? "1" : "0");
    }
    logger.info("* CoverageBitString " + str.toString());
    RuntimeVariable bitStringVariable = CoverageCriteriaAnalyzer.getBitStringVariable(criterion);
    if (goals.isEmpty()) {
        LoggingUtils.getEvoLogger().info("* Coverage of criterion " + criterion + ": 100% (no goals)");
        ClientServices.getInstance().getClientNode().trackOutputVariable(CoverageCriteriaAnalyzer.getCoverageVariable(criterion), 1.0);
        if (bitStringVariable != null) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(bitStringVariable, "1");
        }
    } else {
        double coverage = ((double) covered.cardinality()) / ((double) goals.size());
        LoggingUtils.getEvoLogger().info("* Coverage of criterion " + criterion + ": " + NumberFormat.getPercentInstance().format(coverage));
        LoggingUtils.getEvoLogger().info("* Number of covered goals: " + covered.cardinality() + " / " + goals.size());
        ClientServices.getInstance().getClientNode().trackOutputVariable(CoverageCriteriaAnalyzer.getCoverageVariable(criterion), coverage);
        if (bitStringVariable != null) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(bitStringVariable, str.toString());
        }
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) RuntimeVariable(org.evosuite.statistics.RuntimeVariable) Mutation(org.evosuite.coverage.mutation.Mutation) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 38 with TestChromosome

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

the class RegressionSuiteMinimizer method minimize.

public void minimize(TestSuiteChromosome suite) {
    track(RuntimeVariable.Result_Size, suite.size());
    track(RuntimeVariable.Result_Length, suite.totalLengthOfTestCases());
    track(RuntimeVariable.RSM_OverMinimized, 0);
    logger.warn("Going to minimize test suite. Length: {} ", suite.totalLengthOfTestCases());
    logger.debug("suite: \n{}", suite);
    RegressionTestSuiteChromosome regressionSuite = new RegressionTestSuiteChromosome();
    regressionSuite.addTests(suite.clone().getTestChromosomes());
    // Seems to be broken:
    // removeUnusedVariables(regressionSuite);
    executeSuite(regressionSuite);
    removeDuplicateAssertions(regressionSuite);
    removeDuplicateExceptions(regressionSuite);
    removePassingTests(regressionSuite);
    int testCount = regressionSuite.size();
    minimizeSuite(regressionSuite);
    executeSuite(regressionSuite);
    removePassingTests(regressionSuite);
    sendStats(regressionSuite);
    // Sanity check
    if (regressionSuite.size() == 0 && testCount > 0) {
        track(RuntimeVariable.RSM_OverMinimized, 1);
        logger.error("Test suite over-minimized. Returning non-minimized suite.");
    } else {
        // Adding tests back to the original test suite (if minimization didn't remove all tests)
        suite.clearTests();
        for (TestChromosome t : regressionSuite.getTestChromosomes()) {
            RegressionTestChromosome rtc = (RegressionTestChromosome) t;
            suite.addTest(rtc.getTheTest());
        }
    }
    logger.warn("Minimized Length: {} ", suite.totalLengthOfTestCases());
    logger.debug("suite: \n{}", suite);
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome)

Example 39 with TestChromosome

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

the class RegressionSuiteMinimizer method numFailingAssertions.

private int numFailingAssertions(RegressionTestSuiteChromosome suite) {
    int count = 0;
    for (TestChromosome testChromosome : suite.getTestChromosomes()) {
        RegressionTestChromosome test = (RegressionTestChromosome) testChromosome;
        count += numFailingAssertions(test);
    }
    return count;
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome)

Example 40 with TestChromosome

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

the class RegressionSuiteMinimizer method removeDuplicateAssertions.

private void removeDuplicateAssertions(RegressionTestSuiteChromosome suite) {
    Iterator<TestChromosome> it = suite.getTestChromosomes().iterator();
    Map<String, List<String>> uniqueAssertions = new HashMap<String, List<String>>();
    // int i = -1;
    while (it.hasNext()) {
        // i++;
        RegressionTestChromosome test = (RegressionTestChromosome) it.next();
        boolean changed = false;
        boolean hadAssertion = false;
        // keep track of new unique assertions, and if not unique, remove the assertion
        for (Assertion a : test.getTheTest().getTestCase().getAssertions()) {
            String aClass = a.getClass().getSimpleName();
            List<String> aTypes = uniqueAssertions.get(aClass);
            if (aTypes == null) {
                aTypes = new ArrayList<String>();
            }
            String aType = "";
            if (a instanceof InspectorAssertion) {
                InspectorAssertion ia = (InspectorAssertion) a;
                try {
                    aType = ia.getInspector().getMethod().getName();
                } catch (NullPointerException e) {
                    // technically this should not happen
                    Statement s = ia.getStatement();
                    if (s instanceof MethodStatement) {
                        aType = ((MethodStatement) s).getMethod().getName();
                    }
                }
            }
            if (aTypes.contains(aType)) {
                // logger.warn("removing non-unique assertion: {}-{}", aClass, aType);
                changed = true;
                a.getStatement().getPosition();
                test.getTheTest().getTestCase().removeAssertion(a);
                continue;
            }
            aTypes.add(aType);
            uniqueAssertions.put(aClass, aTypes);
            hadAssertion = true;
        }
        if (changed) {
            test.updateClassloader();
        }
    }
    if (uniqueAssertions.size() > 0) {
        logger.warn("unique assertions: {}", uniqueAssertions);
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) HashMap(java.util.HashMap) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Assertion(org.evosuite.assertion.Assertion) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) ArrayList(java.util.ArrayList) List(java.util.List) 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