Search in sources :

Example 66 with TestChromosome

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

the class CoverageAnalysis method getCoveredGoals.

/**
 * Return the number of covered goals
 *
 * @param testClass
 * @param allGoals
 * @return
 */
public static Set<TestFitnessFunction> getCoveredGoals(Class<?> testClass, List<TestFitnessFunction> allGoals) {
    // A dummy Chromosome
    TestChromosome dummy = new TestChromosome();
    dummy.setChanged(false);
    // Execution result of a dummy Test Case
    ExecutionResult executionResult = new ExecutionResult(dummy.getTestCase());
    Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>();
    List<JUnitResult> results = executeTests(testClass);
    for (JUnitResult testResult : results) {
        executionResult.setTrace(testResult.getExecutionTrace());
        dummy.setLastExecutionResult(executionResult);
        for (TestFitnessFunction goal : allGoals) {
            if (coveredGoals.contains(goal))
                continue;
            else if (goal.isCovered(dummy))
                coveredGoals.add(goal);
        }
    }
    return coveredGoals;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 67 with TestChromosome

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

the class AssertionGenerator method filterFailingAssertions.

public void filterFailingAssertions(TestSuiteChromosome testSuite) {
    List<TestChromosome> tests = testSuite.getTestChromosomes();
    for (TestChromosome test : tests) {
        filterFailingAssertions(test.getTestCase());
    }
    // Execute again in different order
    Randomness.shuffle(tests);
    for (TestChromosome test : tests) {
        filterFailingAssertions(test.getTestCase());
    }
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome)

Example 68 with TestChromosome

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

the class AssertionGenerator method changeClassLoader.

protected void changeClassLoader(TestSuiteChromosome suite) {
    Sandbox.goingToExecuteSUTCode();
    TestGenerationContext.getInstance().goingToExecuteSUTCode();
    Sandbox.goingToExecuteUnsafeCodeOnSameThread();
    try {
        TestGenerationContext.getInstance().resetContext();
        TestGenerationContext.getInstance().goingToExecuteSUTCode();
        // We need to reset the target Class since it requires a different instrumentation
        // for handling assertion generation.
        Properties.resetTargetClass();
        Properties.getInitializedTargetClass();
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Mutants, MutationPool.getMutantCounter());
        for (TestChromosome test : suite.getTestChromosomes()) {
            DefaultTestCase dtest = (DefaultTestCase) test.getTestCase();
            dtest.changeClassLoader(TestGenerationContext.getInstance().getClassLoaderForSUT());
            // clears cached results
            test.setChanged(true);
            test.clearCachedMutationResults();
        }
    } catch (Throwable e) {
        LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
        logger.error("Problem for " + Properties.TARGET_CLASS + ". Full stack:", e);
    } finally {
        TestGenerationContext.getInstance().doneWithExecutingSUTCode();
        Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
        Sandbox.doneWithExecutingSUTCode();
        TestGenerationContext.getInstance().doneWithExecutingSUTCode();
    }
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 69 with TestChromosome

use of org.evosuite.testcase.TestChromosome 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 70 with TestChromosome

use of org.evosuite.testcase.TestChromosome 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)

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