Search in sources :

Example 31 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult 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 32 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class BranchNoveltyFunction method getDistance.

@Override
public double getDistance(TestChromosome individual1, TestChromosome individual2) {
    ExecutionResult result1 = getExecutionResult(individual1);
    ExecutionResult result2 = getExecutionResult(individual2);
    ExecutionTrace trace1 = result1.getTrace();
    ExecutionTrace trace2 = result2.getTrace();
    double difference = 0.0;
    for (Integer branch : branches) {
        if (trace1.hasTrueDistance(branch) && trace2.hasTrueDistance(branch)) {
            double distance1 = trace1.getTrueDistance(branch);
            double distance2 = trace2.getTrueDistance(branch);
            difference += Math.abs(distance1 - distance2);
        } else if (trace1.hasTrueDistance(branch) || trace2.hasTrueDistance(branch)) {
            difference += 1.0;
        }
    }
    Set<String> methods1 = trace1.getCoveredBranchlessMethods();
    Set<String> methods2 = trace2.getCoveredBranchlessMethods();
    for (String branchlessMethod : branchlessMethods) {
        if (methods1.contains(branchlessMethod) != methods2.contains(branchlessMethod)) {
            difference += 1.0;
        }
    }
    difference /= (branches.size() + branchlessMethods.size());
    return difference;
}
Also used : ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 33 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class RegressionAssertionGenerator method runTest.

/**
 * Execute a test case on a mutant
 *
 * @param test The test case that should be executed
 */
@Override
public ExecutionResult runTest(TestCase test) {
    // new ExecutionResult(test, null);
    ExecutionResult result;
    // resetObservers();
    comparisonObserver.clear();
    sameObserver.clear();
    primitiveObserver.clear();
    if (!Properties.REGRESSION_DISABLE_SPECIAL_ASSERTIONS) {
        inspectorObserver.clear();
    }
    fieldObserver.clear();
    nullObserver.clear();
    try {
        logger.debug("Executing test");
        // MutationObserver.activateMutation(mutant);
        result = TestCaseExecutor.getInstance().execute(test);
        // MutationObserver.deactivateMutation(mutant);
        int num = test.size();
        MaxStatementsStoppingCondition.statementsExecuted(num);
        result.setTrace(comparisonObserver.getTrace(), ComparisonTraceEntry.class);
        result.setTrace(sameObserver.getTrace(), SameTraceEntry.class);
        result.setTrace(primitiveObserver.getTrace(), PrimitiveTraceEntry.class);
        if (!Properties.REGRESSION_DISABLE_SPECIAL_ASSERTIONS) {
            result.setTrace(inspectorObserver.getTrace(), InspectorTraceEntry.class);
        }
        result.setTrace(fieldObserver.getTrace(), PrimitiveFieldTraceEntry.class);
        result.setTrace(nullObserver.getTrace(), NullTraceEntry.class);
    } catch (Exception e) {
        throw new Error(e);
    }
    return result;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 34 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class RegressionSuiteMinimizer method executeTest.

/**
 * Execute regression test case on both versions
 *
 * @param regressionTest regression test chromosome to be executed on both versions
 */
private void executeTest(RegressionTestChromosome regressionTest) {
    TestChromosome testChromosome = regressionTest.getTheTest();
    TestChromosome otherChromosome = regressionTest.getTheSameTestForTheOtherClassLoader();
    ExecutionResult result = regressionAssertionGenerator.runTest(testChromosome.getTestCase());
    ExecutionResult otherResult = regressionAssertionGenerator.runTest(otherChromosome.getTestCase());
    regressionTest.setLastExecutionResult(result);
    regressionTest.setLastRegressionExecutionResult(otherResult);
    testChromosome.setLastExecutionResult(result);
    otherChromosome.setLastExecutionResult(otherResult);
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 35 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class JUnitTestSuite method runTest.

/**
 * <p>runTest</p>
 *
 * @param test a {@link org.evosuite.testcase.TestCase} object.
 * @return a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 */
public ExecutionResult runTest(TestCase test) {
    ExecutionResult result = new ExecutionResult(test, null);
    try {
        logger.debug("Executing test");
        result = executor.execute(test);
        int num = test.size();
        MaxStatementsStoppingCondition.statementsExecuted(num);
    // result.touched.addAll(HOMObserver.getTouched());
    } catch (Exception e) {
        throw new Error(e);
    }
    return result;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Aggregations

ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)93 TestChromosome (org.evosuite.testcase.TestChromosome)33 HashSet (java.util.HashSet)15 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)15 ArrayList (java.util.ArrayList)11 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)10 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 TestCase (org.evosuite.testcase.TestCase)9 VariableReference (org.evosuite.testcase.variable.VariableReference)9 Test (org.junit.Test)9 LinkedHashSet (java.util.LinkedHashSet)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)7 ArrayReference (org.evosuite.testcase.variable.ArrayReference)7 Set (java.util.Set)6 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)6 Map (java.util.Map)4 Entry (java.util.Map.Entry)3 ExecutionTrace (org.evosuite.testcase.execution.ExecutionTrace)3