Search in sources :

Example 21 with ExecutionResult

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

the class TestFitnessFunction method isCovered.

/**
 * <p>
 * isCovered
 * </p>
 *
 * @param tc
 *            a {@link org.evosuite.testcase.TestChromosome} object.
 * @return a boolean.
 */
public boolean isCovered(TestChromosome tc) {
    if (tc.getTestCase().isGoalCovered(this)) {
        return true;
    }
    ExecutionResult result = tc.getLastExecutionResult();
    if (result == null || tc.isChanged()) {
        result = runTest(tc.test);
        tc.setLastExecutionResult(result);
        tc.setChanged(false);
    }
    return isCovered(tc, result);
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 22 with ExecutionResult

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

the class CarvingManager method readTestCases.

private void readTestCases() throws IllegalStateException {
    ClientServices.getInstance().getClientNode().changeState(ClientState.CARVING);
    Collection<String> junitTestNames = getListOfJUnitClassNames();
    LoggingUtils.getEvoLogger().info("* Executing tests from {} test {} for carving", junitTestNames.size(), junitTestNames.size() == 1 ? "class" : "classes");
    final JUnitCore runner = new JUnitCore();
    final CarvingRunListener listener = new CarvingRunListener();
    runner.addListener(listener);
    final List<Class<?>> junitTestClasses = new ArrayList<Class<?>>();
    final org.evosuite.testcarver.extraction.CarvingClassLoader classLoader = new org.evosuite.testcarver.extraction.CarvingClassLoader();
    // TODO: This really needs to be done in a nicer way!
    FieldRegistry.carvingClassLoader = classLoader;
    try {
        // instrument target class
        classLoader.loadClass(Properties.TARGET_CLASS);
    } catch (final ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    for (String className : junitTestNames) {
        String classNameWithDots = ResourceList.getClassNameFromResourcePath(className);
        try {
            final Class<?> junitClass = classLoader.loadClass(classNameWithDots);
            junitTestClasses.add(junitClass);
        } catch (ClassNotFoundException e) {
            logger.error("Failed to load JUnit test class {}: {}", classNameWithDots, e);
        }
    }
    final Class<?>[] classes = new Class<?>[junitTestClasses.size()];
    junitTestClasses.toArray(classes);
    Result result = runner.run(classes);
    logger.info("Result: {}/{}", result.getFailureCount(), result.getRunCount());
    for (Failure failure : result.getFailures()) {
        logger.info("Failure: {}", failure.getMessage());
        logger.info("Exception: {}", failure.getException());
    }
    Map<Class<?>, List<TestCase>> testMap = listener.getTestCases();
    for (Class<?> targetClass : testMap.keySet()) {
        List<TestCase> processedTests = new ArrayList<>();
        for (TestCase test : testMap.get(targetClass)) {
            String testName = ((CarvedTestCase) test).getName();
            if (test.isEmpty())
                continue;
            ExecutionResult executionResult = null;
            try {
                executionResult = TestCaseExecutor.runTest(test);
            } catch (Throwable t) {
                logger.info("Error while executing carved test {}: {}", testName, t);
                continue;
            }
            if (executionResult.noThrownExceptions()) {
                logger.info("Adding carved test without exception");
                logger.info(test.toCode());
                processedTests.add(test);
            } else {
                logger.info("Exception thrown in carved test {}: {}", testName, executionResult.getExceptionThrownAtPosition(executionResult.getFirstPositionOfThrownException()));
                for (StackTraceElement elem : executionResult.getExceptionThrownAtPosition(executionResult.getFirstPositionOfThrownException()).getStackTrace()) {
                    logger.info(elem.toString());
                }
                logger.info(test.toCode(executionResult.exposeExceptionMapping()));
                if (Properties.CHOP_CARVED_EXCEPTIONS) {
                    logger.info("Chopping exception of carved test");
                    chopException(test, executionResult);
                    if (test.hasObject(Properties.getTargetClassAndDontInitialise(), test.size())) {
                        processedTests.add(test);
                    } else {
                        logger.info("Chopped test is empty");
                    }
                } else {
                    logger.info("Not adding carved test with exception: ");
                }
            }
        }
        if (processedTests.size() > 0) {
            LoggingUtils.getEvoLogger().info(" -> Carved {} tests for class {} from existing JUnit tests", processedTests.size(), targetClass);
            if (logger.isDebugEnabled()) {
                for (TestCase test : processedTests) {
                    logger.debug("Carved Test {}: {}", ((CarvedTestCase) test).getName(), test.toCode());
                }
            }
        } else {
            // String outcome = "";
            // for (Failure failure : result.getFailures()) {
            // outcome += "(" + failure.getDescription() + ", " + failure.getTrace()
            // + ") ";
            // }
            logger.info("It was not possible to carve any test case for class {} from {}", targetClass.getName(), Arrays.toString(junitTestNames.toArray()));
        // + ". Test execution results: " + outcome);
        }
        carvedTests.put(targetClass, processedTests);
    }
    carvingDone = true;
    // TODO: Argh.
    FieldRegistry.carvingClassLoader = null;
// TODO:
// ClientNodeLocal client = ClientServices.getInstance().getClientNode();
// client.trackOutputVariable(RuntimeVariable.CarvedTests, totalNumberOfTestsCarved);
// client.trackOutputVariable(RuntimeVariable.CarvedCoverage,carvedCoverage);
}
Also used : JUnitCore(org.junit.runner.JUnitCore) CarvedTestCase(org.evosuite.testcarver.testcase.CarvedTestCase) ArrayList(java.util.ArrayList) Result(org.junit.runner.Result) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) ArrayList(java.util.ArrayList) List(java.util.List) ResourceList(org.evosuite.classpath.ResourceList) Failure(org.junit.runner.notification.Failure) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) CarvedTestCase(org.evosuite.testcarver.testcase.CarvedTestCase) TestCase(org.evosuite.testcase.TestCase)

Example 23 with ExecutionResult

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

the class ConstantInliner method inline.

/**
 * <p>
 * inline
 * </p>
 *
 * @param suite
 *            a {@link org.evosuite.testsuite.TestSuiteChromosome} object.
 */
public void inline(TestSuiteChromosome suite) {
    for (TestChromosome test : suite.getTestChromosomes()) {
        final int old_test_size = test.size();
        inline(test);
        final int new_test_size = test.size();
        final int removed_statements = old_test_size - new_test_size;
        if (removed_statements > 0) {
            ExecutionResult lastExecResult = test.getLastExecutionResult();
            if (lastExecResult != null) {
                final int old_exec_statements = lastExecResult.getExecutedStatements();
                final int new_exec_statements = old_exec_statements - removed_statements;
                lastExecResult.setExecutedStatements(new_exec_statements);
            }
        }
    }
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 24 with ExecutionResult

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

the class TestDefaultValue method testLong.

@Test
public void testLong() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference longArray0 = builder.appendArrayStmt(Long[].class, 10);
    VariableReference long0 = builder.appendNull(Long.class);
    builder.appendAssignment(longArray0, 0, long0);
    builder.appendAssignment(long0, longArray0, 0);
    builder.appendMethod(long0, Long.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 25 with ExecutionResult

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

the class TestDefaultValue method testFloat.

@Test
public void testFloat() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference floatArray0 = builder.appendArrayStmt(Float[].class, 10);
    VariableReference float0 = builder.appendNull(Float.class);
    builder.appendAssignment(floatArray0, 0, float0);
    builder.appendAssignment(float0, floatArray0, 0);
    builder.appendMethod(float0, Float.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    System.out.println(tc.toCode());
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

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