Search in sources :

Example 71 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class RandomLengthTestFactory method getRandomTestCase.

/**
 * Create a random individual
 *
 * @param size
 */
private TestCase getRandomTestCase(int size) {
    boolean tracerEnabled = ExecutionTracer.isEnabled();
    if (tracerEnabled)
        ExecutionTracer.disable();
    TestCase test = getNewTestCase();
    int num = 0;
    // Choose a random length in 0 - size
    int length = Randomness.nextInt(size);
    while (length == 0) length = Randomness.nextInt(size);
    TestFactory testFactory = TestFactory.getInstance();
    // Then add random stuff
    while (test.size() < length && num < Properties.MAX_ATTEMPTS) {
        testFactory.insertRandomStatement(test, test.size() - 1);
        num++;
    }
    if (logger.isDebugEnabled())
        logger.debug("Randomized test case:" + test.toCode());
    if (tracerEnabled)
        ExecutionTracer.enable();
    return test;
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFactory(org.evosuite.testcase.TestFactory)

Example 72 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class DiversityObserver method getSuiteSimilarity.

/**
 * Naive similarity comparison between suites simply consists of merging all tests to a single test
 * for each suite, and then comparing these tests
 *
 * @param suite1
 * @param suite2
 * @return
 */
public static double getSuiteSimilarity(TestSuiteChromosome suite1, TestSuiteChromosome suite2) {
    TestCase test1 = new DefaultTestCase();
    for (TestCase test : suite1.getTests()) {
        for (Statement s : test) {
            // These are not valid tests as the variables still point to the original test
            // but that doesn't matter as we're not executing the test
            test1.addStatement(s);
        }
    }
    TestCase test2 = new DefaultTestCase();
    for (TestCase test : suite2.getTests()) {
        for (Statement s : test) {
            test2.addStatement(s);
        }
    }
    return getNeedlemanWunschScore(test1, test2);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase)

Example 73 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactory method getChromosome.

@Override
public TestChromosome getChromosome() {
    final int N_mutations = Properties.SEED_MUTATIONS;
    final double P_clone = Properties.SEED_CLONE;
    double r = Randomness.nextDouble();
    if (r >= P_clone || junitTests.isEmpty()) {
        logger.debug("Using random test");
        return defaultFactory.getChromosome();
    }
    // Cloning
    logger.info("Cloning user test");
    TestCase test = Randomness.choice(junitTests);
    TestChromosome chromosome = new TestChromosome();
    chromosome.setTestCase(test.clone());
    if (N_mutations > 0) {
        int numMutations = Randomness.nextInt(N_mutations);
        logger.debug("Mutations: " + numMutations);
        // Delta
        for (int i = 0; i < numMutations; i++) {
            chromosome.mutate();
        }
    }
    return chromosome;
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 74 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class DeprecatedTestSuiteDSE method applyDSE0.

/**
 * Attempt to negate individual branches until budget is used up, or there
 * are no further branches to negate
 *
 * @param individual
 */
public boolean applyDSE0(TestSuiteChromosome individual) {
    logger.info("[DSE] Current test suite: " + individual.toString());
    boolean wasSuccess = false;
    // expansion already happens as part of LS
    // TestSuiteChromosome expandedTests = expandTestSuite(individual);
    TestSuiteChromosome expandedTests = individual.clone();
    createPathConstraints(expandedTests);
    // fitness.getFitness(expandedTests);
    double originalFitness = getFitness(individual);
    while (hasNextBranchCondition() && !LocalSearchBudget.getInstance().isFinished()) {
        logger.info("Branches remaining: " + unsolvedBranchConditions.size());
        TestBranchPair next = getNextBranchCondition();
        BranchCondition branch = next.branch;
        List<BranchCondition> pathCondition = next.pathCondition;
        List<Constraint<?>> reachingConstraints = new LinkedList<Constraint<?>>();
        for (BranchCondition b : pathCondition) {
            reachingConstraints.addAll(b.getSupportingConstraints());
            if (b == branch) {
                break;
            }
            reachingConstraints.add(b.getConstraint());
        }
        List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
        TestCase newTest = negateCondition(new HashSet<Constraint<?>>(reachingConstraints), branch.getConstraint(), next.test.getTestCase());
        if (newTest != null) {
            logger.info("Found new test: " + newTest.toCode());
            TestChromosome newTestChromosome = new TestChromosome();
            newTestChromosome.setTestCase(newTest);
            expandedTests.addTest(newTestChromosome);
            if (Properties.DSE_KEEP_ALL_TESTS) {
                updatePathConstraints(newTestChromosome);
                calculateUncoveredBranches();
                individual.addTest(newTest);
                wasSuccess = true;
            } else {
                if (getFitness(expandedTests) < originalFitness) {
                    logger.info("New test improves fitness to {}", getFitness(expandedTests));
                    DSEStats.getInstance().reportNewTestUseful();
                    wasSuccess = true;
                    // no need to clone so we can keep executionresult
                    updatePathConstraints(newTestChromosome);
                    calculateUncoveredBranches(newTestChromosome);
                    individual.addTest(newTest);
                    originalFitness = getFitness(expandedTests);
                // TODO: Cancel on fitness 0 - would need to know if
                // ZeroFitness is a stopping condition
                } else {
                    logger.info("New test does not improve fitness");
                    DSEStats.getInstance().reportNewTestUnuseful();
                    expandedTests.deleteTest(newTest);
                }
            }
            success++;
        } else {
            unsolvableBranchConditions.add(branch);
            failed++;
            logger.info("Failed to find new test.");
        }
    }
    logger.info("Finished DSE");
    // Ensure fitness values are up to date.
    getFitness(individual);
    LocalSearchBudget.getInstance().countLocalSearchOnTestSuite();
    return wasSuccess;
}
Also used : Constraint(org.evosuite.symbolic.expr.Constraint) TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BranchCondition(org.evosuite.symbolic.BranchCondition) TestChromosome(org.evosuite.testcase.TestChromosome) LinkedList(java.util.LinkedList)

Example 75 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class MockJOptionPaneShowInternalOptionDialogTest method testShowInternalInputDialogs.

@Test
public void testShowInternalInputDialogs() 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)

Aggregations

TestCase (org.evosuite.testcase.TestCase)192 Test (org.junit.Test)119 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)90 ArrayList (java.util.ArrayList)67 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)47 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)45 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)43 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)39 VariableReference (org.evosuite.testcase.variable.VariableReference)26 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)25 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)25 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)24 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)17 TestChromosome (org.evosuite.testcase.TestChromosome)17 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)15 Ignore (org.junit.Ignore)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)14 GenericMethod (org.evosuite.utils.generic.GenericMethod)12 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)11 InputCoverageGoal (org.evosuite.coverage.io.input.InputCoverageGoal)10