Search in sources :

Example 51 with ExecutionResult

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

the class LineCoverageSuiteFitness method analyzeTraces.

/**
 * Iterate over all execution results and summarize statistics
 *
 * @param results
 * @param coveredLines
 * @return
 */
private boolean analyzeTraces(List<ExecutionResult> results, Set<Integer> coveredLines) {
    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 (Integer goalID : this.lineGoals.keySet()) {
            TestFitnessFunction goal = this.lineGoals.get(goalID);
            // archive is updated by the TestFitnessFunction class
            double fit = goal.getFitness(test, result);
            if (fit == 0.0) {
                // helper to count the number of covered goals
                coveredLines.add(goalID);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveLines.add(goalID);
            }
        }
    }
    return hasTimeoutOrTestException;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 52 with ExecutionResult

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

the class MethodCoverageSuiteFitness method analyzeTraces.

/**
 * Iterate over all execution results and summarize statistics
 *
 * @param results
 * @param calledMethods
 * @return
 */
protected boolean analyzeTraces(List<ExecutionResult> results, Set<String> calledMethods) {
    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 (String methodName : this.methodCoverageMap.keySet()) {
            TestFitnessFunction goal = this.methodCoverageMap.get(methodName);
            // archive is updated by the TestFitnessFunction class
            double fit = goal.getFitness(test, result);
            if (fit == 0.0) {
                // helper to count the number of covered goals
                calledMethods.add(methodName);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveMethods.add(methodName);
            }
        }
        // In case there were exceptions in a constructor
        handleConstructorExceptions(test, result, calledMethods);
    }
    return hasTimeoutOrTestException;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 53 with ExecutionResult

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

the class AbstractMOSA method deriveCoveredExceptions.

/**
 * This method analyzes the execution results of a TestChromosome looking for generated exceptions.
 * Such exceptions are converted in instances of the class {@link ExceptionCoverageTestFitness},
 * which are additional covered goals when using as criterion {@link Properties.Criterion.EXCEPTION}
 * @param t TestChromosome to analyze
 * @return list of exception goals being covered by t
 */
public List<ExceptionCoverageTestFitness> deriveCoveredExceptions(T t) {
    List<ExceptionCoverageTestFitness> covered_exceptions = new ArrayList<ExceptionCoverageTestFitness>();
    TestChromosome testCh = (TestChromosome) t;
    ExecutionResult result = testCh.getLastExecutionResult();
    Map<String, Set<Class<?>>> implicitTypesOfExceptions = new LinkedHashMap<>();
    Map<String, Set<Class<?>>> explicitTypesOfExceptions = new LinkedHashMap<>();
    Map<String, Set<Class<?>>> declaredTypesOfExceptions = new LinkedHashMap<>();
    for (Integer i : result.getPositionsWhereExceptionsWereThrown()) {
        if (ExceptionCoverageHelper.shouldSkip(result, i)) {
            continue;
        }
        Class<?> exceptionClass = ExceptionCoverageHelper.getExceptionClass(result, i);
        // eg name+descriptor
        String methodIdentifier = ExceptionCoverageHelper.getMethodIdentifier(result, i);
        // was the exception originated by a direct call on the SUT?
        boolean sutException = ExceptionCoverageHelper.isSutException(result, i);
        if (sutException) {
            boolean notDeclared = !ExceptionCoverageHelper.isDeclared(result, i);
            if (notDeclared) {
                /*
					 * 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).
					 */
                boolean isExplicit = ExceptionCoverageHelper.isExplicit(result, i);
                if (isExplicit) {
                    if (!explicitTypesOfExceptions.containsKey(methodIdentifier)) {
                        explicitTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
                    }
                    explicitTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
                } else {
                    if (!implicitTypesOfExceptions.containsKey(methodIdentifier)) {
                        implicitTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
                    }
                    implicitTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
                }
            } else {
                if (!declaredTypesOfExceptions.containsKey(methodIdentifier)) {
                    declaredTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
                }
                declaredTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
            }
            ExceptionCoverageTestFitness.ExceptionType type = ExceptionCoverageHelper.getType(result, i);
            /*
				 * Add goal to list of fitness functions to solve
				 */
            ExceptionCoverageTestFitness goal = new ExceptionCoverageTestFitness(Properties.TARGET_CLASS, methodIdentifier, exceptionClass, type);
            covered_exceptions.add(goal);
        }
    }
    return covered_exceptions;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 54 with ExecutionResult

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

the class MutationTestFitness method runTest.

/**
 * <p>
 * runTest
 * </p>
 *
 * @param test
 *            a {@link org.evosuite.testcase.TestCase} object.
 * @param mutant
 *            a {@link org.evosuite.coverage.mutation.Mutation} object.
 * @return a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 */
public static ExecutionResult runTest(TestCase test, Mutation mutant) {
    ExecutionResult result = new ExecutionResult(test, mutant);
    try {
        if (mutant != null)
            logger.debug("Executing test for mutant " + mutant.getId() + ": \n" + test.toCode());
        else
            logger.debug("Executing test witout mutant");
        if (mutant != null)
            MutationObserver.activateMutation(mutant);
        result = TestCaseExecutor.getInstance().execute(test);
        if (mutant != null)
            MutationObserver.deactivateMutation(mutant);
        int num = test.size();
        if (!result.noThrownExceptions()) {
            num = result.getFirstPositionOfThrownException();
        }
        // if (mutant == null)
        MaxStatementsStoppingCondition.statementsExecuted(num);
    } catch (Exception e) {
        throw new Error(e);
    }
    return result;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) IOException(java.io.IOException)

Example 55 with ExecutionResult

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

the class OnlyMutationSuiteFitness method getFitness.

/* (non-Javadoc)
	 * @see org.evosuite.ga.FitnessFunction#getFitness(org.evosuite.ga.Chromosome)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> individual) {
    /**
     * e.g. classes with only static constructors
     */
    if (this.numMutants == 0) {
        updateIndividual(this, individual, 0.0);
        ((TestSuiteChromosome) individual).setCoverage(this, 1.0);
        ((TestSuiteChromosome) individual).setNumOfCoveredGoals(this, 0);
        return 0.0;
    }
    List<ExecutionResult> results = runTestSuite(individual);
    double fitness = 0.0;
    Map<Integer, Double> mutant_distance = new LinkedHashMap<Integer, Double>();
    Set<Integer> touchedMutants = new LinkedHashSet<Integer>();
    for (ExecutionResult result : results) {
        // use reflection for basic criteria, not for mutation
        if (result.hasTimeout() || result.hasTestException() || result.calledReflection()) {
            continue;
        }
        touchedMutants.addAll(result.getTrace().getTouchedMutants());
        Map<Integer, Double> touchedMutantsDistances = result.getTrace().getMutationDistances();
        if (touchedMutantsDistances.isEmpty()) {
            // if 'result' does not touch any mutant, no need to continue
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        Iterator<Entry<Integer, MutationTestFitness>> it = this.mutantMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Integer, MutationTestFitness> entry = it.next();
            int mutantID = entry.getKey();
            TestFitnessFunction goal = entry.getValue();
            double fit = 0.0;
            if (touchedMutantsDistances.containsKey(mutantID)) {
                fit = touchedMutantsDistances.get(mutantID);
                if (!mutant_distance.containsKey(mutantID)) {
                    mutant_distance.put(mutantID, fit);
                } else {
                    mutant_distance.put(mutantID, Math.min(mutant_distance.get(mutantID), fit));
                }
            } else {
                // archive is updated by the TestFitnessFunction class
                fit = goal.getFitness(test, result);
            }
            if (fit == 0.0) {
                // update list of covered goals
                test.getTestCase().addCoveredGoal(goal);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveMutants.add(mutantID);
            }
            if (Properties.TEST_ARCHIVE) {
                Archive.getArchiveInstance().updateArchive(goal, test, fit);
            }
        }
    }
    // Second objective: touch all mutants?
    fitness += MutationPool.getMutantCounter() - touchedMutants.size();
    int covered = this.removedMutants.size();
    for (Double distance : mutant_distance.values()) {
        if (distance < 0) {
            logger.warn("Distance is " + distance + " / " + Integer.MAX_VALUE + " / " + Integer.MIN_VALUE);
            // FIXXME
            distance = 0.0;
        }
        fitness += normalize(distance);
        if (distance == 0.0)
            covered++;
    }
    updateIndividual(this, individual, fitness);
    ((TestSuiteChromosome) individual).setCoverage(this, (double) covered / (double) this.numMutants);
    ((TestSuiteChromosome) individual).setNumOfCoveredGoals(this, covered);
    return fitness;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) Entry(java.util.Map.Entry) AbstractTestSuiteChromosome(org.evosuite.testsuite.AbstractTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

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