Search in sources :

Example 31 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class MockJOptionPaneShowMessageDialogTest method testShowMessageDialog1.

@Test
public void testShowMessageDialog1() throws Exception {
    TestSuiteChromosome suite = new TestSuiteChromosome();
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t0 = buildTestCase1TrueBranch(cl);
    TestCase t1 = buildTestCase1FalseBranch(cl);
    suite.addTest(t0);
    suite.addTest(t1);
    BranchCoverageSuiteFitness ff = new BranchCoverageSuiteFitness(cl);
    ff.getFitness(suite);
    Set<TestFitnessFunction> coveredGoals = suite.getCoveredGoals();
    Assert.assertEquals(3, coveredGoals.size());
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 32 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class MockJOptionPaneShowOptionDialogTest method testShowInputDialogs.

@Test
public void testShowInputDialogs() throws Exception {
    TestSuiteChromosome suite = new TestSuiteChromosome();
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t1 = buildTestCase0(cl);
    suite.addTest(t1);
    BranchCoverageSuiteFitness ff = new BranchCoverageSuiteFitness(cl);
    ff.getFitness(suite);
    Set<TestFitnessFunction> coveredGoals = suite.getCoveredGoals();
    Assert.assertEquals(2, coveredGoals.size());
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 33 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class MockJOptionPaneTest method testCoveredGoals.

@Test
public void testCoveredGoals() throws Exception {
    Properties.TIMEOUT = Integer.MAX_VALUE;
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t0 = buildTestCase0(cl);
    TestCase t1 = buildTestCase1(cl);
    TestSuiteChromosome suite = new TestSuiteChromosome();
    suite.addTest(t0);
    suite.addTest(t1);
    BranchCoverageSuiteFitness ff = new BranchCoverageSuiteFitness(cl);
    ff.getFitness(suite);
    Set<TestFitnessFunction> coveredGoals = suite.getCoveredGoals();
    Assert.assertEquals(3, coveredGoals.size());
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 34 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class RegressionSuiteSerializer method getAppendedRegressionSuiteArchive.

/**
 * Get (and append) a coverage-based test suite archive for regression testing
 */
private static TestSuiteChromosome getAppendedRegressionSuiteArchive() {
    List<TestChromosome> previousArchive = TestSuiteSerialization.loadTests(REGRESSION_ARCHIVE_FILE);
    LoggingUtils.getEvoLogger().info("* previousArchive.size(): " + previousArchive.size());
    previousArchive.forEach(t -> t.getTestCase().removeAssertions());
    // execute previous regression test archive
    removeTestsThatDoNotcompile(previousArchive);
    Properties.TEST_ARCHIVE = false;
    TestSuiteChromosome archiveSuite = new TestSuiteChromosome();
    archiveSuite.addTests(previousArchive);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness(TestGenerationContext.getInstance().getClassLoaderForSUT());
    // execute the test suite
    branchCoverageSuiteFitness.getFitness(archiveSuite);
    LoggingUtils.getEvoLogger().info("* archive covered goals: " + archiveSuite.getCoveredGoals().size());
    Properties.TEST_ARCHIVE = true;
    TestSuiteChromosome testArchive = Archive.getArchiveInstance().mergeArchiveAndSolution(archiveSuite);
    LoggingUtils.getEvoLogger().info("* newArchive.size(): " + testArchive.size());
    LoggingUtils.getEvoLogger().info("* new covered goals: " + testArchive.getCoveredGoals().size());
    // add all assertions
    AssertionStrategy tmpStrategy = Properties.ASSERTION_STRATEGY;
    Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
    TestSuiteGeneratorHelper.addAssertions(testArchive);
    Properties.ASSERTION_STRATEGY = tmpStrategy;
    return testArchive;
}
Also used : BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestChromosome(org.evosuite.testcase.TestChromosome) AssertionStrategy(org.evosuite.Properties.AssertionStrategy)

Example 35 with BranchCoverageSuiteFitness

use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.

the class RegressionSuiteStrategy method generateRandomRegressionTests.

private TestSuiteChromosome generateRandomRegressionTests() {
    LoggingUtils.getEvoLogger().info("* Using RANDOM regression test generation");
    if (Properties.KEEP_REGRESSION_ARCHIVE) {
        Properties.TEST_ARCHIVE = true;
    }
    RegressionTestSuiteChromosome suite = new RegressionTestSuiteChromosome();
    PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
    GeneticAlgorithm<?> suiteGA = algorithmFactory.getSearchAlgorithm();
    // statistics.searchStarted(suiteGA);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness(TestGenerationContext.getInstance().getClassLoaderForSUT());
    // regressionMonitor.searchStarted(suiteGA);
    RegressionTestChromosomeFactory factory = new RegressionTestChromosomeFactory();
    LoggingUtils.getEvoLogger().warn("*** generating RANDOM regression tests");
    // TODO: Shutdown hook?
    List<TestFitnessFunction> goals = getGoals(true);
    track(RuntimeVariable.Total_Goals, goals.size());
    StoppingCondition stoppingCondition = getStoppingCondition();
    // fitnessFunction.getFitness(suite);
    int totalTestCount = 0;
    int usefulTestCount = 0;
    int simulatedAge = 0;
    int numAssertions = 0;
    int executedStatemets = 0;
    boolean firstTry = true;
    // Properties.REGRESSION_RANDOM_STRATEGY:
    // 0: skip evaluation after first find, dont keep tests
    // 1: dont skip evaluation after first find, dont keep tests
    // 2: dont skip evaluation after first find, keep tests
    // 3: skip evaluation after first find, keep tests [default]
    long startTime = System.currentTimeMillis();
    while (!stoppingCondition.isFinished() || (numAssertions != 0)) {
        if (numAssertions == 0 || Properties.REGRESSION_RANDOM_STRATEGY == 1 || Properties.REGRESSION_RANDOM_STRATEGY == 2) {
            RegressionTestChromosome test = factory.getChromosome();
            RegressionTestSuiteChromosome clone = new RegressionTestSuiteChromosome();
            clone.addTest(test);
            List<TestCase> testCases = clone.getTests();
            // fitnessFunction.getFitness(clone);
            /*
         * logger.debug("Old fitness: {}, new fitness: {}",
         * suite.getFitness(), clone.getFitness());
         */
            executedStatemets += test.size();
            numAssertions = RegressionAssertionCounter.getNumAssertions(clone);
            if (Properties.KEEP_REGRESSION_ARCHIVE) {
                branchCoverageSuiteFitness.getFitness(clone.getTestSuite());
            }
            if (numAssertions > 0) {
                LoggingUtils.getEvoLogger().warn("Generated test with {} assertions.", numAssertions);
            }
            totalTestCount++;
            if (numAssertions > 0) {
                numAssertions = 0;
                // boolean compilable = JUnitAnalyzer.verifyCompilationAndExecution(testCases);
                JUnitAnalyzer.removeTestsThatDoNotCompile(testCases);
                JUnitAnalyzer.handleTestsThatAreUnstable(testCases);
                if (testCases.size() > 0) {
                    clone = new RegressionTestSuiteChromosome();
                    for (TestCase t : testCases) {
                        RegressionTestChromosome rtc = new RegressionTestChromosome();
                        if (t.isUnstable()) {
                            continue;
                        }
                        TestChromosome tc = new TestChromosome();
                        tc.setTestCase(t);
                        rtc.setTest(tc);
                        clone.addTest(rtc);
                    }
                    // test.set
                    // clone.addTest(testCases);
                    numAssertions = RegressionAssertionCounter.getNumAssertions(clone, false, false);
                    LoggingUtils.getEvoLogger().warn("Keeping {} assertions.", numAssertions);
                    if (numAssertions > 0) {
                        usefulTestCount++;
                        suite.addTest(test);
                    }
                } else {
                    LoggingUtils.getEvoLogger().warn("ignored assertions. tests were removed.");
                }
            }
        } else {
            if (numAssertions > 0) {
                break;
            }
        /*
        try {
                Thread.sleep(1000);
        } catch (InterruptedException e) {
                e.printStackTrace();
        }
        */
        }
        // regressionMonitor.iteration(suiteGA);
        if (firstTry || (System.currentTimeMillis() - startTime) >= 4000) {
            startTime = System.currentTimeMillis();
            simulatedAge++;
            firstTry = false;
        }
    }
    // regressionMonitor.searchFinished(suiteGA);
    LoggingUtils.getEvoLogger().warn("*** Random test generation finished.");
    LoggingUtils.getEvoLogger().warn("*=*=*=* Total tests: {} | Tests with assertion: {}", totalTestCount, usefulTestCount);
    // statistics.searchFinished(suiteGA);
    zero_fitness.setFinished();
    LoggingUtils.getEvoLogger().info("* Generated " + suite.size() + " tests with total length " + suite.totalLengthOfTestCases());
    goals = getGoals(false);
    track(RuntimeVariable.Total_Goals, goals.size());
    suiteGA.printBudget();
    if (!(Properties.REGRESSION_RANDOM_STRATEGY == 2 || Properties.REGRESSION_RANDOM_STRATEGY == 3)) {
        suite = new RegressionTestSuiteChromosome();
    }
    TestSuiteChromosome bestSuites = new TestSuiteChromosome();
    for (TestCase t : suite.getTests()) {
        bestSuites.addTest(t);
    }
    return bestSuites;
}
Also used : RegressionTestChromosomeFactory(org.evosuite.regression.RegressionTestChromosomeFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) MaxStatementsStoppingCondition(org.evosuite.ga.stoppingconditions.MaxStatementsStoppingCondition) ZeroFitnessStoppingCondition(org.evosuite.ga.stoppingconditions.ZeroFitnessStoppingCondition) StoppingCondition(org.evosuite.ga.stoppingconditions.StoppingCondition) TestCase(org.evosuite.testcase.TestCase) RegressionTestChromosome(org.evosuite.regression.RegressionTestChromosome) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) RegressionTestChromosome(org.evosuite.regression.RegressionTestChromosome)

Aggregations

BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)52 Test (org.junit.Test)45 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)18 TestCase (org.evosuite.testcase.TestCase)17 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)16 TestChromosome (org.evosuite.testcase.TestChromosome)14 Properties (org.evosuite.Properties)12 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)12 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)12 Method (java.lang.reflect.Method)11 GenericMethod (org.evosuite.utils.generic.GenericMethod)11 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)10 ArrayList (java.util.ArrayList)8 BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)7 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)7 VariableReference (org.evosuite.testcase.variable.VariableReference)7 GenericClass (org.evosuite.utils.generic.GenericClass)7 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)7 Foo (com.examples.with.different.packagename.symbolic.Foo)6