Search in sources :

Example 36 with ExecutionResult

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

the class CoverageGoalTestNameGenerationStrategy method getUpdatedResults.

/**
 * Some criteria require re-execution with observers. Make sure the results are up-to-date
 *
 * @param requiredCriteria
 * @param origResults
 * @return
 */
private List<ExecutionResult> getUpdatedResults(List<Properties.Criterion> requiredCriteria, List<ExecutionResult> origResults) {
    List<ExecutionObserver> newObservers = new ArrayList<ExecutionObserver>();
    if (requiredCriteria.contains(Properties.Criterion.INPUT)) {
        newObservers.add(new InputObserver());
    }
    if (requiredCriteria.contains(Properties.Criterion.OUTPUT)) {
        newObservers.add(new OutputObserver());
    }
    if (newObservers.isEmpty()) {
        return origResults;
    }
    for (ExecutionObserver observer : newObservers) TestCaseExecutor.getInstance().addObserver(observer);
    List<ExecutionResult> newResults = new ArrayList<ExecutionResult>();
    for (ExecutionResult result : origResults) {
        ExecutionResult newResult = TestCaseExecutor.getInstance().runTest(result.test);
        newResults.add(newResult);
    }
    for (ExecutionObserver observer : newObservers) TestCaseExecutor.getInstance().removeObserver(observer);
    return newResults;
}
Also used : ExecutionObserver(org.evosuite.testcase.execution.ExecutionObserver) OutputObserver(org.evosuite.coverage.io.output.OutputObserver) InputObserver(org.evosuite.coverage.io.input.InputObserver) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 37 with ExecutionResult

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

the class TestSuiteWriter method runTest.

/**
 * <p>
 * runTest
 * </p>
 *
 * @param test a {@link org.evosuite.testcase.TestCase} object.
 * @return a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 */
protected ExecutionResult runTest(TestCase test) {
    ExecutionResult result = new ExecutionResult(test, null);
    try {
        logger.debug("Executing test");
        result = executor.execute(test);
    } catch (Exception e) {
        throw new Error(e);
    }
    return result;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 38 with ExecutionResult

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

the class Scaffolding method generateMockInitialization.

/**
 * This is needed  because the first time we do initialize a mock object, that can take
 * some seconds (successive calls would be based on cached data), and so tests might
 * timeout. So here we force the mock initialization in a @BeforeClass
 *
 * @param bd
 * @param results
 */
private void generateMockInitialization(String testClassName, StringBuilder bd, List<ExecutionResult> results) {
    if (!TestSuiteWriterUtils.doesUseMocks(results)) {
        return;
    }
    // In order to make sure this is called *after* initializeClasses this method is now called directly from initEvoSuiteFramework
    // bd.append(METHOD_SPACE);
    // bd.append("@BeforeClass \n");
    bd.append(METHOD_SPACE);
    bd.append("private static void initMocksToAvoidTimeoutsInTheTests() throws ClassNotFoundException { \n");
    Set<String> mockStatements = new LinkedHashSet<>();
    for (ExecutionResult er : results) {
        for (Statement st : er.test) {
            if (st instanceof FunctionalMockStatement) {
                FunctionalMockStatement fms = (FunctionalMockStatement) st;
                String name = new GenericClass(fms.getReturnType()).getRawClass().getTypeName();
                mockStatements.add("mock(Class.forName(\"" + name + "\", false, " + testClassName + ".class.getClassLoader()));");
            }
        }
    }
    mockStatements.stream().sorted().forEach(m -> {
        bd.append(BLOCK_SPACE);
        bd.append(m);
        bd.append("\n");
    });
    bd.append(METHOD_SPACE);
    bd.append("}\n");
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) Statement(org.evosuite.testcase.statements.Statement) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement)

Example 39 with ExecutionResult

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

the class ArrayLocalSearch method searchLength.

private boolean searchLength(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    boolean hasImproved = false;
    ArrayStatement p = (ArrayStatement) test.getTestCase().getStatement(statement);
    logger.debug("Performing local search on array length, starting with length {}", p.size());
    ExecutionResult oldResult = test.getLastExecutionResult();
    oldLength = p.size();
    boolean done = false;
    while (!done) {
        done = true;
        // Try +1
        p.setSize(oldLength + 1);
        logger.debug("Trying increment of {}", p.getCode());
        if (objective.hasImproved(test)) {
            done = false;
            hasImproved = true;
            boolean improved = true;
            while (improved) {
                oldLength = p.size();
                oldResult = test.getLastExecutionResult();
                p.setSize(oldLength + 1);
                logger.debug("Trying increment of {}", p.getCode());
                improved = objective.hasImproved(test);
            }
            p.setSize(oldLength);
            test.setLastExecutionResult(oldResult);
            test.setChanged(false);
        } else {
            if (oldLength > 0) {
                // Restore original, try -1
                p.setSize(oldLength);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
                p.setSize(oldLength - 1);
            } else {
                p.setSize(Properties.MAX_ARRAY);
            }
            logger.debug("Trying decrement of {}", p.getCode());
            if (objective.hasImproved(test)) {
                done = false;
                hasImproved = true;
                boolean improved = true;
                while (improved && p.size() > 0) {
                    oldLength = p.size();
                    oldResult = test.getLastExecutionResult();
                    p.setSize(oldLength - 1);
                    logger.debug("Trying decrement of {}", p.getCode());
                    improved = objective.hasImproved(test);
                }
                p.setSize(oldLength);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
            } else {
                p.setSize(oldLength);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
            }
        }
    }
    logger.debug("Finished local array length search with result {}", p.getCode());
    return hasImproved;
}
Also used : ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 40 with ExecutionResult

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

the class FloatLocalSearch method doSearch.

private int doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective, double initialDelta, double factor, NumericalPrimitiveStatement<T> p) {
    int changed = 0;
    oldValue = p.getValue();
    ExecutionResult oldResult = test.getLastExecutionResult();
    boolean done = false;
    while (!done) {
        done = true;
        // Try +1
        p.increment(initialDelta);
        logger.info("Trying increment of " + p.getCode());
        // logger.info(" -> " + p.getCode());
        int change = objective.hasChanged(test);
        if (change != 0)
            changed = change;
        if (change < 0) {
            done = false;
            // changed = true;
            iterate(factor * initialDelta, factor, objective, test, p, statement);
            oldValue = p.getValue();
            oldResult = test.getLastExecutionResult();
        } else {
            // Restore original, try -1
            p.setValue(oldValue);
            test.setLastExecutionResult(oldResult);
            test.setChanged(false);
            p.increment(-initialDelta);
            logger.info("Trying decrement of " + p.getCode());
            // logger.info(" -> " + p.getCode());
            change = objective.hasChanged(test);
            if (change < 0) {
                logger.info("Iterating because of improvement");
                changed = change;
                done = false;
                iterate(-factor * initialDelta, factor, objective, test, p, statement);
                oldValue = p.getValue();
                oldResult = test.getLastExecutionResult();
            } else {
                logger.info("Not iterating because no improvement");
                p.setValue(oldValue);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
            }
        }
    }
    logger.debug("Finished local search with result " + p.getCode());
    return changed;
}
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