Search in sources :

Example 26 with TestFitnessFunction

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

the class CoverageGoalTestNameGenerationStrategy method generateNames.

/**
 * Helper method that does the bulk of the work
 * @param testToGoals
 */
private void generateNames(Map<TestCase, Set<TestFitnessFunction>> testToGoals) {
    initializeMethodCoverageCount(testToGoals);
    findUniqueGoals(testToGoals);
    initializeNameGoals(testToGoals);
    // Iteratively add goals as long as there are resolvable ambiguities
    boolean changed = true;
    while (changed) {
        setTestNames(testToGoals);
        Map<String, Set<TestCase>> dupTestNameMap = determineDuplicateNames();
        // For each duplicate set, add new test goals
        changed = false;
        for (Map.Entry<String, Set<TestCase>> entry : dupTestNameMap.entrySet()) {
            if (entry.getKey().length() >= MAX_CHARS) {
                continue;
            }
            // Try adding something unique for the given test set
            if (resolveAmbiguity(testToGoals, entry.getValue(), true))
                changed = true;
            else {
                // the number of tests in this ambiguity group
                if (resolveAmbiguity(testToGoals, entry.getValue(), false))
                    changed = true;
            }
        }
    }
    // If there is absolutely nothing unique, add the top goals so that the test at least has a name
    for (Map.Entry<TestCase, Set<TestFitnessFunction>> entry : testToGoals.entrySet()) {
        if (entry.getValue().isEmpty()) {
            List<TestFitnessFunction> goals = new ArrayList<>(getUniqueNonMethodGoals(entry.getKey(), testToGoals));
            if (goals.isEmpty()) {
                goals.addAll(filterSupportedGoals(entry.getKey().getCoveredGoals()));
            }
            Collections.sort(goals, new GoalComparator());
            if (!goals.isEmpty()) {
                TestFitnessFunction goal = goals.iterator().next();
                entry.getValue().add(goal);
                String name = getTestName(entry.getKey(), entry.getValue());
                testToName.put(entry.getKey(), name);
            }
        }
    }
    // Add numbers to remaining duplicate names
    fixAmbiguousTestNames();
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 27 with TestFitnessFunction

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

the class CoverageGoalTestNameGenerationStrategy method getUniqueNonMethodGoals.

/**
 * There is nothing unique about the test, so we have to add goals until we find a unique name
 */
private Set<TestFitnessFunction> getUniqueNonMethodGoals(TestCase test, Map<TestCase, Set<TestFitnessFunction>> testToGoals) {
    Set<TestFitnessFunction> allGoals = new LinkedHashSet<>();
    for (Set<TestFitnessFunction> goals : testToGoals.values()) {
        allGoals.addAll(goals);
    }
    Set<TestFitnessFunction> goals = new LinkedHashSet<>();
    for (TestFitnessFunction goal : filterSupportedGoals(test.getCoveredGoals())) {
        if (goal instanceof MethodCoverageTestFitness)
            continue;
        if (goal instanceof MethodNoExceptionCoverageTestFitness)
            continue;
        if (!allGoals.contains(goal)) {
            goals.add(goal);
        }
    }
    return goals;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) MethodNoExceptionCoverageTestFitness(org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness)

Example 28 with TestFitnessFunction

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

the class TestSuiteChromosome method getCoveredGoals.

/**
 * <p>
 * getCoveredGoals
 * </p>
 *
 * @return a {@link java.util.Set} object.
 */
public Set<TestFitnessFunction> getCoveredGoals() {
    Set<TestFitnessFunction> goals = new LinkedHashSet<TestFitnessFunction>();
    for (TestChromosome test : tests) {
        final Set<TestFitnessFunction> goalsForTest = test.getTestCase().getCoveredGoals();
        goals.addAll(goalsForTest);
    }
    return goals;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 29 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction 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)

Example 30 with TestFitnessFunction

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

the class MockJOptionPaneShowMessageDialogTest method testShowMessageDialog0.

@Test
public void testShowMessageDialog0() throws Exception {
    TestSuiteChromosome suite = new TestSuiteChromosome();
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t0 = buildTestCase0TrueBranch(cl);
    TestCase t1 = buildTestCase0FalseBranch(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)

Aggregations

TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)73 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)26 TestCase (org.evosuite.testcase.TestCase)24 Test (org.junit.Test)22 TestChromosome (org.evosuite.testcase.TestChromosome)21 ArrayList (java.util.ArrayList)20 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)11 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)8 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)8 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)8 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)4 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)4 Properties (org.evosuite.Properties)3