Search in sources :

Example 66 with ExecutionResult

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

the class StatisticsSender method executedAndThenSendIndividualToMaster.

/**
 * First execute (if needed) the test cases to be sure to have latest correct data,
 * and then send it to Master
 */
public static void executedAndThenSendIndividualToMaster(TestSuiteChromosome testSuite) throws IllegalArgumentException {
    if (testSuite == null) {
        throw new IllegalArgumentException("No defined test suite to send");
    }
    if (!Properties.NEW_STATISTICS)
        return;
    for (TestChromosome test : testSuite.getTestChromosomes()) {
        if (test.getLastExecutionResult() == null) {
            ExecutionResult result = TestCaseExecutor.runTest(test.getTestCase());
            test.setLastExecutionResult(result);
        }
    }
    sendCoveredInfo(testSuite);
    sendExceptionInfo(testSuite);
    sendIndividualToMaster(testSuite);
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 67 with ExecutionResult

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

the class EntBugTestStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    // Set up search algorithm
    LoggingUtils.getEvoLogger().info("* Setting up search algorithm for individual test generation (ASE'13)");
    ExecutionTracer.enableTraceCalls();
    // Set up genetic algorithm
    PropertiesTestGAFactory factory = new PropertiesTestGAFactory();
    GeneticAlgorithm<TestChromosome> ga = factory.getSearchAlgorithm();
    if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD) {
        TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(ga);
    }
    // What's the search target
    RhoCoverageFactory rhoFactory = (RhoCoverageFactory) FitnessFunctions.getFitnessFactory(Properties.Criterion.RHO);
    RhoCoverageTestFitness rhoTestFitnessFunction = new RhoCoverageTestFitness();
    ga.addFitnessFunction(rhoTestFitnessFunction);
    // Goals
    List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>(rhoFactory.getCoverageGoals());
    LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
    LoggingUtils.getEvoLogger().info("  - Rho " + goals.size());
    double previous_fitness = RhoCoverageFactory.getRho();
    double best_fitness = 0.0;
    int number_of_generations = (int) (Properties.SEARCH_BUDGET / 10);
    // Properties.SEARCH_BUDGET = 10; // 10 seconds for each generation
    TestSuiteChromosome bests = new TestSuiteChromosome();
    while (number_of_generations > 0) {
        LoggingUtils.getEvoLogger().info("  * iteration(" + number_of_generations + ")");
        // 10 seconds for each generation
        // FIXME: should be a parameter?
        ga.setStoppingConditionLimit(10);
        ga.resetStoppingConditions();
        ga.clearPopulation();
        // ga.setChromosomeFactory(getDefaultChromosomeFactory()); // not in the original version
        ga.generateSolution();
        number_of_generations--;
        TestChromosome best = (TestChromosome) ga.getBestIndividual();
        if (best.getLastExecutionResult() == null) {
            // some timeout?
            continue;
        }
        best_fitness = best.getFitness(rhoTestFitnessFunction);
        if (// we've found a better test case
        (best_fitness < previous_fitness) || // or this new test case is not so bad (i.e., < Properties.EPSON)
        (best_fitness <= Properties.EPSON)) {
            // GOOD
            LoggingUtils.getEvoLogger().info("  * new best (previous fitness: " + previous_fitness + " | best_fitness: " + best_fitness + ")");
            ExecutionResult exec = best.getLastExecutionResult();
            ExecutionTrace trace = exec.getTrace();
            Set<Integer> testCoverage = trace.getCoveredLines();
            LoggingUtils.getEvoLogger().info("  * new test case added " + testCoverage.toString());
            rhoTestFitnessFunction.incrementNumber_of_Ones(testCoverage.size());
            rhoTestFitnessFunction.incrementNumber_of_Test_Cases();
            rhoTestFitnessFunction.addTestCoverage(testCoverage);
            bests.addTest(best);
            // update global fitness
            previous_fitness = best_fitness;
        } else {
            // BAD
            LoggingUtils.getEvoLogger().info("  * new test case ignored (previous fitness: " + previous_fitness + " | best_fitness: " + best_fitness + ")");
        }
    }
    LoggingUtils.getEvoLogger().info("* Search finished after, best individual has fitness " + best_fitness);
    LoggingUtils.getEvoLogger().info("Resulting test suite: " + bests.size() + " tests, length " + bests.totalLengthOfTestCases());
    return bests;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) RhoCoverageTestFitness(org.evosuite.coverage.rho.RhoCoverageTestFitness) RhoCoverageFactory(org.evosuite.coverage.rho.RhoCoverageFactory) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 68 with ExecutionResult

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

the class SourceExceptionsSystemTest method testRuntimeException.

@Test
public void testRuntimeException() {
    String targetClass = SourceExceptions.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.REPLACE_CALLS = true;
    Properties.CRITERION = new Properties.Criterion[] { Properties.Criterion.LINE, Properties.Criterion.EXCEPTION };
    EvoSuite evosuite = new EvoSuite();
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    Assert.assertNotNull(best);
    TestCaseExecutor.getInstance().initExecutor();
    for (TestChromosome test : best.getTestChromosomes()) {
        ExecutionResult executionResult = TestCaseExecutor.getInstance().runTest(test.getTestCase());
        test.setLastExecutionResult(executionResult);
    }
    String code = best.toString();
    Assert.assertTrue("Code:\n" + code, code.contains("verifyException(\"com.examples.with.different.packagename.mock.java.lang.SourceExceptions\","));
    Assert.assertTrue("Code:\n" + code, code.contains("verifyException(\"com.examples.with.different.packagename.mock.java.lang.SourceExceptions$Foo\","));
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 69 with ExecutionResult

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

the class RegressionAssertionCounter method checkForAssertions.

private static int checkForAssertions(Boolean removeAssertions, Boolean noExecution, RegressionAssertionGenerator assertionGenerator, RegressionTestChromosome regressionTest) {
    int totalCount = 0;
    if (!noExecution) {
        ExecutionResult result1 = assertionGenerator.runTest(regressionTest.getTheTest().getTestCase());
        ExecutionResult result2 = assertionGenerator.runTest(regressionTest.getTheSameTestForTheOtherClassLoader().getTestCase());
        if (result1.test == null || result2.test == null || result1.hasTimeout() || result2.hasTimeout()) {
            logger.warn("=============================== HAD TIMEOUT ===============================");
        } else {
            int exceptionDiffs = RegressionExceptionHelper.compareExceptionDiffs(result1.getCopyOfExceptionMapping(), result2.getCopyOfExceptionMapping());
            if (exceptionDiffs > 0) {
                logger.debug("Had {} different exceptions! ({})", exceptionDiffs, totalCount);
            }
            totalCount += exceptionDiffs;
            for (Class<?> observerClass : RegressionAssertionGenerator.observerClasses) {
                if (result1.getTrace(observerClass) != null) {
                    result1.getTrace(observerClass).getAssertions(regressionTest.getTheTest().getTestCase(), result2.getTrace(observerClass));
                }
            }
        }
    }
    int assertionCount = regressionTest.getTheTest().getTestCase().getAssertions().size();
    totalCount += assertionCount;
    // Store assertion comments for later flakiness check
    if (assertionCount > 0) {
        List<Assertion> assertions = regressionTest.getTheTest().getTestCase().getAssertions();
        List<String> assertionComments = new ArrayList<>();
        for (Assertion assertion : assertions) {
            logger.warn("+++++ Assertion: {} {}", assertion.getCode(), assertion.getComment());
            assertionComments.add(assertion.getComment());
        }
        RegressionAssertionCounter.assertionComments.put(regressionTest.getTheTest().getTestCase().toCode().hashCode(), assertionComments);
        if (assertions.size() == 0) {
            logger.warn("=========> NO ASSERTIONS!!!");
        } else {
            logger.warn("Assertions ^^^^^^^^^");
        }
    }
    if (removeAssertions) {
        regressionTest.getTheTest().getTestCase().removeAssertions();
    }
    return totalCount;
}
Also used : Assertion(org.evosuite.assertion.Assertion) ArrayList(java.util.ArrayList) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 70 with ExecutionResult

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

the class RegressionSuiteFitness method executeChangedTestsAndUpdateResults.

private void executeChangedTestsAndUpdateResults(AbstractTestSuiteChromosome<? extends ExecutableChromosome> changedSuite) {
    observer.clearPools();
    diversityMap.clear();
    RegressionTestSuiteChromosome suite = (RegressionTestSuiteChromosome) changedSuite;
    for (TestChromosome chromosome : suite.getTestChromosomes()) {
        RegressionTestChromosome c = (RegressionTestChromosome) chromosome;
        observer.enable();
        observer.resetObjPool();
        observer.setRegressionFlag(false);
        TestChromosome testChromosome = c.getTheTest();
        TestChromosome otherChromosome = c.getTheSameTestForTheOtherClassLoader();
        // Only execute test if it hasn't been changed
        if (testChromosome.isChanged() || testChromosome.getLastExecutionResult() == null) {
            // record diversity
            if (Properties.REGRESSION_DIVERSITY) {
                RegressionFitnessHelper.trackDiversity(c, testChromosome);
            }
            ExecutionResult result = TestCaseExecutor.runTest(testChromosome.getTestCase());
            observer.setRegressionFlag(true);
            ExecutionResult otherResult = TestCaseExecutor.runTest(otherChromosome.getTestCase());
            observer.setRegressionFlag(false);
            observer.disable();
            double objectDistance = getTestObjectDistance(observer.currentObjectMapPool, observer.currentRegressionObjectMapPool);
            result.regressionObjectDistance = objectDistance;
            otherResult.regressionObjectDistance = objectDistance;
            testChromosome.setLastExecutionResult(result);
            testChromosome.setChanged(false);
            otherChromosome.setLastExecutionResult(otherResult);
            otherChromosome.setChanged(false);
        }
        if (Properties.REGRESSION_DIVERSITY) {
            measureDiversity(c);
        }
    }
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) 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