Search in sources :

Example 71 with ExecutionResult

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

the class RegressionSuiteFitness method getFitness.

/*
   * Get fitness value for individual
   * 
   * @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
   */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
    if (useMeasure(RegressionMeasure.STATE_DIFFERENCE)) {
        TestCaseExecutor.getInstance().addObserver(observer);
        observer.clearPools();
    }
    double distance = 0.0;
    double fitness = 0.0;
    // populate branches with a value of 2 (branch not covered yet)
    // branchDistanceMap = new HashMap<Integer, Double>();
    branchDistanceMap = (Map<Integer, Double>) tempBranchDistanceMap.clone();
    int numDifferentExceptions = 0;
    int totalExceptions = 0;
    executeChangedTestsAndUpdateResults(individual);
    RegressionTestSuiteChromosome suite = (RegressionTestSuiteChromosome) individual;
    List<Double> objectDistances = new ArrayList<>();
    for (TestChromosome regressionTest : suite.getTestChromosomes()) {
        RegressionTestChromosome rtc = (RegressionTestChromosome) regressionTest;
        ExecutionResult result1 = rtc.getTheTest().getLastExecutionResult();
        ExecutionResult result2 = rtc.getTheSameTestForTheOtherClassLoader().getLastExecutionResult();
        // calculating exception difference
        int numExceptionOrig = result1.getNumberOfThrownExceptions();
        int numExceptionReg = result2.getNumberOfThrownExceptions();
        double exDiff = Math.abs((double) (numExceptionOrig - numExceptionReg));
        totalExceptions += numExceptionOrig + numExceptionReg;
        numDifferentExceptions += exDiff;
        // branch distance
        if (useMeasure(RegressionMeasure.BRANCH_DISTANCE)) {
            this.getBranchDistance(result1.getTrace().getMethodCalls(), result2.getTrace().getMethodCalls());
        }
        // object distance
        objectDistances.add(result1.regressionObjectDistance);
    }
    double objectDistanceFitness = 0;
    if (useMeasure(RegressionMeasure.STATE_DIFFERENCE)) {
        if (!objectDistances.isEmpty()) {
            distance = Collections.max(objectDistances);
        }
        objectDistanceFitness = (1.0 / (1.0 + distance)) * (maxBranchFitnessValueO + maxBranchFitnessValueR);
    }
    AbstractTestSuiteChromosome<TestChromosome> testSuiteChromosome = suite.getTestSuite();
    AbstractTestSuiteChromosome<TestChromosome> testRegressionSuiteChromosome = null;
    if (useMeasure(RegressionMeasure.COVERAGE_NEW)) {
        testRegressionSuiteChromosome = suite.getTestSuiteForTheOtherClassLoader();
    }
    double coverageOld = 0, coverageNew = 0;
    if (useMeasure(RegressionMeasure.COVERAGE_OLD)) {
        coverageOld = bcFitness.getFitness(testSuiteChromosome);
    }
    if (useMeasure(RegressionMeasure.COVERAGE_NEW)) {
        coverageNew = bcFitnessRegression.getFitness(testRegressionSuiteChromosome);
    }
    double coverage = coverageOld + coverageNew;
    double branchDistanceFitness = 0;
    double totalBranchDistanceFitness = 0.0;
    if (useMeasure(RegressionMeasure.BRANCH_DISTANCE)) {
        for (Map.Entry<Integer, Double> branch : branchDistanceMap.entrySet()) {
            totalBranchDistanceFitness += branch.getValue();
        }
        branchDistanceFitness = totalBranchDistanceFitness;
    }
    switch(Properties.REGRESSION_FITNESS) {
        case COVERAGE_OLD:
            fitness += coverageOld;
            break;
        case COVERAGE_NEW:
            fitness += coverageNew;
            break;
        case BRANCH_DISTANCE:
            fitness += branchDistanceFitness;
            break;
        case STATE_DIFFERENCE:
            fitness += objectDistanceFitness;
            break;
        case COVERAGE:
            fitness += coverage;
            break;
        case ALL_MEASURES:
        default:
            fitness += coverage;
            fitness += branchDistanceFitness;
            fitness += objectDistanceFitness;
            break;
    }
    double exceptionDistance = (1.0 / (1.0 + numDifferentExceptions));
    fitness += exceptionDistance;
    if (Properties.REGRESSION_DIVERSITY) {
        calculateDiversity();
        double diversityFitness = (1.0 / (1.0 + uniqueCalls));
        fitness += diversityFitness;
    }
    individual.setCoverage(this, (bcFitness.totalCovered + bcFitnessRegression.totalCovered) / 2.0);
    updateIndividual(this, individual, fitness);
    if (fitness < bestFitness) {
        bestFitness = fitness;
        logger.warn("OBJ distance: " + distance + " - fitness:" + fitness + " - branchDistance:" + totalBranchDistanceFitness + " - coverage:" + coverage + " - ex: " + numDifferentExceptions + " - tex: " + totalExceptions);
        logger.warn("Best Fitness " + fitness + ", number of tests: " + testSuiteChromosome.size() + ", total length: " + testSuiteChromosome.totalLengthOfTestCases());
    }
    return fitness;
}
Also used : ArrayList(java.util.ArrayList) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome) HashMap(java.util.HashMap) Map(java.util.Map)

Example 72 with ExecutionResult

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

the class RegressionTestFitnessFunction method runTest.

/**
 * Execute a test case
 *
 * @param testChromosome The test case to execute
 * @return Result of the execution
 */
public ExecutionResult runTest(TestChromosome testChromosome) {
    if (testChromosome.getLastExecutionResult() != null && !testChromosome.isChanged()) {
        return testChromosome.getLastExecutionResult();
    }
    TestCase test = testChromosome.getTestCase();
    ExecutionResult result = new ExecutionResult(test, null);
    try {
        result = TestCaseExecutor.getInstance().execute(test);
        int num = test.size();
        if (!result.noThrownExceptions()) {
            num = result.getFirstPositionOfThrownException();
        }
        MaxStatementsStoppingCondition.statementsExecuted(num);
    // for(TestObserver observer : observers) {
    // observer.testResult(result);
    // }
    } catch (Exception e) {
        logger.error("TG: Exception caught: ", e);
        throw new RuntimeException(e);
    }
    return result;
}
Also used : TestCase(org.evosuite.testcase.TestCase) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 73 with ExecutionResult

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

the class JUnitTestSuite method runSuite.

/**
 * <p>runSuite</p>
 *
 * @param chromosome a {@link org.evosuite.testsuite.TestSuiteChromosome} object.
 */
public void runSuite(TestSuiteChromosome chromosome) {
    coveredMethods = new HashSet<String>();
    coveredBranchesTrue = new HashSet<Integer>();
    coveredBranchesFalse = new HashSet<Integer>();
    for (TestCase test : chromosome.getTests()) {
        ExecutionResult result = runTest(test);
        coveredMethods.addAll(result.getTrace().getCoveredMethods());
        coveredBranchesTrue.addAll(result.getTrace().getCoveredTrueBranches());
        coveredBranchesFalse.addAll(result.getTrace().getCoveredFalseBranches());
    }
}
Also used : TestCase(org.evosuite.testcase.TestCase) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 74 with ExecutionResult

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

the class CoverageGoalTestNameGenerationStrategy method addGoalsNotIncludedInTargetCriteria.

/**
 * Name generation assumes that certain coverage criteria are included. If we haven't targeted them yet,
 * we need to determine the covered goals. This may require re-executing the tests with observers.
 *
 * @param results
 */
private void addGoalsNotIncludedInTargetCriteria(List<ExecutionResult> results) {
    List<Properties.Criterion> requiredCriteria = new ArrayList<>(Arrays.asList(new Properties.Criterion[] { Properties.Criterion.OUTPUT, Properties.Criterion.INPUT, Properties.Criterion.METHOD, Properties.Criterion.METHODNOEXCEPTION, Properties.Criterion.EXCEPTION }));
    requiredCriteria.removeAll(Arrays.asList(Properties.CRITERION));
    results = getUpdatedResults(requiredCriteria, results);
    for (Properties.Criterion c : requiredCriteria) {
        TestFitnessFactory<? extends TestFitnessFunction> goalFactory = FitnessFunctions.getFitnessFactory(c);
        List<? extends TestFitnessFunction> goals = goalFactory.getCoverageGoals();
        for (ExecutionResult result : results) {
            for (TestFitnessFunction goal : goals) {
                if (goal.isCovered(result))
                    result.test.addCoveredGoal(goal);
            }
        }
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) Properties(org.evosuite.Properties)

Example 75 with ExecutionResult

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

the class BranchNoveltyFunction method getExecutionResult.

private ExecutionResult getExecutionResult(TestChromosome individual) {
    ExecutionResult origResult = individual.getLastExecutionResult();
    if (origResult == null || individual.isChanged()) {
        origResult = runTest(individual.getTestCase());
        individual.setLastExecutionResult(origResult);
        individual.setChanged(false);
    }
    return individual.getLastExecutionResult();
}
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