Search in sources :

Example 1 with RegressionSuiteMinimizer

use of org.evosuite.regression.RegressionSuiteMinimizer in project evosuite by EvoSuite.

the class TestSuiteGenerator method postProcessTests.

/**
 * Apply any readability optimizations and other techniques that should use
 * or modify the generated tests
 *
 * @param testSuite
 */
protected void postProcessTests(TestSuiteChromosome testSuite) {
    // If overall time is short, the search might not have had enough time
    // to come up with a suite without timeouts. However, they will slow
    // down
    // the rest of the process, and may lead to invalid tests
    testSuite.getTestChromosomes().removeIf(t -> t.getLastExecutionResult() != null && (t.getLastExecutionResult().hasTimeout() || t.getLastExecutionResult().hasTestException()));
    if (Properties.CTG_SEEDS_FILE_OUT != null) {
        TestSuiteSerialization.saveTests(testSuite, new File(Properties.CTG_SEEDS_FILE_OUT));
    } else if (Properties.TEST_FACTORY == TestFactory.SERIALIZATION) {
        TestSuiteSerialization.saveTests(testSuite, new File(Properties.SEED_DIR + File.separator + Properties.TARGET_CLASS));
    }
    /*
		 * Remove covered goals that are not part of the minimization targets,
		 * as they might screw up coverage analysis when a minimization timeout
		 * occurs. This may happen e.g. when MutationSuiteFitness calls
		 * BranchCoverageSuiteFitness which adds branch goals.
		 */
    // TODO: This creates an inconsistency between
    // suite.getCoveredGoals().size() and suite.getNumCoveredGoals()
    // but it is not clear how to update numcoveredgoals
    List<TestFitnessFunction> goals = new ArrayList<>();
    for (TestFitnessFactory<?> ff : getFitnessFactories()) {
        goals.addAll(ff.getCoverageGoals());
    }
    for (TestFitnessFunction f : testSuite.getCoveredGoals()) {
        if (!goals.contains(f)) {
            testSuite.removeCoveredGoal(f);
        }
    }
    if (Properties.INLINE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.INLINING);
        ConstantInliner inliner = new ConstantInliner();
        // progressMonitor.setCurrentPhase("Inlining constants");
        // Map<FitnessFunction<? extends TestSuite<?>>, Double> fitnesses =
        // testSuite.getFitnesses();
        inliner.inline(testSuite);
    }
    if (Properties.MINIMIZE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.MINIMIZATION);
        // progressMonitor.setCurrentPhase("Minimizing test cases");
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping minimization because not enough time is left");
            ClientServices.track(RuntimeVariable.Result_Size, testSuite.size());
            ClientServices.track(RuntimeVariable.Minimized_Size, testSuite.size());
            ClientServices.track(RuntimeVariable.Result_Length, testSuite.totalLengthOfTestCases());
            ClientServices.track(RuntimeVariable.Minimized_Length, testSuite.totalLengthOfTestCases());
        } else if (Properties.isRegression()) {
            RegressionSuiteMinimizer minimizer = new RegressionSuiteMinimizer();
            minimizer.minimize(testSuite);
        } else {
            double before = testSuite.getFitness();
            TestSuiteMinimizer minimizer = new TestSuiteMinimizer(getFitnessFactories());
            LoggingUtils.getEvoLogger().info("* Minimizing test suite");
            minimizer.minimize(testSuite, true);
            double after = testSuite.getFitness();
            if (after > before + 0.01d) {
                // assume minimization
                throw new Error("EvoSuite bug: minimization lead fitness from " + before + " to " + after);
            }
        }
    } else {
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping minimization because not enough time is left");
        }
        ClientServices.track(RuntimeVariable.Result_Size, testSuite.size());
        ClientServices.track(RuntimeVariable.Minimized_Size, testSuite.size());
        ClientServices.track(RuntimeVariable.Result_Length, testSuite.totalLengthOfTestCases());
        ClientServices.track(RuntimeVariable.Minimized_Length, testSuite.totalLengthOfTestCases());
    }
    if (Properties.COVERAGE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.COVERAGE_ANALYSIS);
        CoverageCriteriaAnalyzer.analyzeCoverage(testSuite);
    }
    double coverage = testSuite.getCoverage();
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
    // SearchStatistics.getInstance().mutationScore(coverage);
    }
    StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
    LoggingUtils.getEvoLogger().info("* Generated " + testSuite.size() + " tests with total length " + testSuite.totalLengthOfTestCases());
    // TODO: In the end we will only need one analysis technique
    if (!Properties.ANALYSIS_CRITERIA.isEmpty()) {
        // SearchStatistics.getInstance().addCoverage(Properties.CRITERION.toString(),
        // coverage);
        CoverageCriteriaAnalyzer.analyzeCriteria(testSuite, Properties.ANALYSIS_CRITERIA);
    // FIXME: can we send all bestSuites?
    }
    if (Properties.CRITERION.length > 1)
        LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage) + " (average coverage for all fitness functions)");
    else
        LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage));
    // printBudget(ga); // TODO - need to move this somewhere else
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) && Properties.ANALYSIS_CRITERIA.isEmpty())
        DefUseCoverageSuiteFitness.printCoverage();
    DSEStats.getInstance().trackConstraintTypes();
    DSEStats.getInstance().trackSolverStatistics();
    if (Properties.DSE_PROBABILITY > 0.0 && Properties.LOCAL_SEARCH_RATE > 0 && Properties.LOCAL_SEARCH_PROBABILITY > 0.0) {
        DSEStats.getInstance().logStatistics();
    }
    if (Properties.FILTER_SANDBOX_TESTS) {
        for (TestChromosome test : testSuite.getTestChromosomes()) {
            // delete all statements leading to security exceptions
            ExecutionResult result = test.getLastExecutionResult();
            if (result == null) {
                result = TestCaseExecutor.runTest(test.getTestCase());
            }
            if (result.hasSecurityException()) {
                int position = result.getFirstPositionOfThrownException();
                if (position > 0) {
                    test.getTestCase().chop(position);
                    result = TestCaseExecutor.runTest(test.getTestCase());
                    test.setLastExecutionResult(result);
                }
            }
        }
    }
    if (Properties.ASSERTIONS && !Properties.isRegression()) {
        LoggingUtils.getEvoLogger().info("* Generating assertions");
        // progressMonitor.setCurrentPhase("Generating assertions");
        ClientServices.getInstance().getClientNode().changeState(ClientState.ASSERTION_GENERATION);
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping assertion generation because not enough time is left");
        } else {
            TestSuiteGeneratorHelper.addAssertions(testSuite);
        }
        // FIXME: can we
        StatisticsSender.sendIndividualToMaster(testSuite);
    // pass the list
    // of
    // testsuitechromosomes?
    }
    if (Properties.NO_RUNTIME_DEPENDENCY) {
        LoggingUtils.getEvoLogger().info("* Property NO_RUNTIME_DEPENDENCY is set to true - skipping JUnit compile check");
        LoggingUtils.getEvoLogger().info("* WARNING: Not including the runtime dependencies is likely to lead to flaky tests!");
    } else if (Properties.JUNIT_TESTS && Properties.JUNIT_CHECK) {
        compileAndCheckTests(testSuite);
    }
    if (Properties.SERIALIZE_REGRESSION_TEST_SUITE) {
        RegressionSuiteSerializer.appendToRegressionTestSuite(testSuite);
    }
    if (Properties.isRegression() && Properties.KEEP_REGRESSION_ARCHIVE) {
        RegressionSuiteSerializer.storeRegressionArchive();
    }
}
Also used : ConstantInliner(org.evosuite.testcase.ConstantInliner) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RegressionSuiteMinimizer(org.evosuite.regression.RegressionSuiteMinimizer) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) File(java.io.File) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

File (java.io.File)1 RegressionSuiteMinimizer (org.evosuite.regression.RegressionSuiteMinimizer)1 ConstantInliner (org.evosuite.testcase.ConstantInliner)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1