Search in sources :

Example 31 with TestFitnessFunction

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

the class MockJOptionPaneShowMessageDialogTest method testShowMessageDialog2.

@Test
public void testShowMessageDialog2() throws Exception {
    TestSuiteChromosome suite = new TestSuiteChromosome();
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t0 = buildTestCase2TrueBranch(cl);
    TestCase t1 = buildTestCase2FalseBranch(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 TestFitnessFunction

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

the class CoverageAnalysis method getCoveredGoals.

/**
 * Return the number of covered goals
 *
 * @param testClass
 * @param allGoals
 * @return
 */
public static Set<TestFitnessFunction> getCoveredGoals(Class<?> testClass, List<TestFitnessFunction> allGoals) {
    // A dummy Chromosome
    TestChromosome dummy = new TestChromosome();
    dummy.setChanged(false);
    // Execution result of a dummy Test Case
    ExecutionResult executionResult = new ExecutionResult(dummy.getTestCase());
    Set<TestFitnessFunction> coveredGoals = new HashSet<TestFitnessFunction>();
    List<JUnitResult> results = executeTests(testClass);
    for (JUnitResult testResult : results) {
        executionResult.setTrace(testResult.getExecutionTrace());
        dummy.setLastExecutionResult(executionResult);
        for (TestFitnessFunction goal : allGoals) {
            if (coveredGoals.contains(goal))
                continue;
            else if (goal.isCovered(dummy))
                coveredGoals.add(goal);
        }
    }
    return coveredGoals;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 33 with TestFitnessFunction

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

the class TestGoalComparator method testComparatorList.

@Test
public void testComparatorList() {
    GoalComparator comparator = new GoalComparator();
    MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString()");
    ExceptionCoverageTestFitness goal2 = new ExceptionCoverageTestFitness("FooClass", "toString()", MockArithmeticException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT);
    OutputCoverageGoal outputGoal = new OutputCoverageGoal("FooClass", "toString", Type.getType("Ljava.lang.String;"), IOCoverageConstants.REF_NONNULL);
    OutputCoverageTestFitness goal3 = new OutputCoverageTestFitness(outputGoal);
    List<TestFitnessFunction> goals = new ArrayList<>();
    goals.add(goal1);
    goals.add(goal2);
    goals.add(goal3);
    Collections.sort(goals, comparator);
    assertEquals(goal2, goals.get(0));
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) GoalComparator(org.evosuite.junit.naming.methods.GoalComparator) Test(org.junit.Test)

Example 34 with TestFitnessFunction

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

the class TestMethodNamingComplexExamples method testThreeTestsOutputGoals.

@Test
public void testThreeTestsOutputGoals() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // Method goals
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "mist(I)V");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "keys()");
    // Output goals for method keys()I
    TestFitnessFunction goal3 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "keys()", Type.INT_TYPE, NUM_POSITIVE));
    TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "keys()", Type.INT_TYPE, NUM_NEGATIVE));
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal2);
    test2.addCoveredGoal(goal3);
    DefaultTestCase test3 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test3.addStatement(new IntPrimitiveStatement(test3, 3));
    test3.addCoveredGoal(goal1);
    test3.addCoveredGoal(goal2);
    test3.addCoveredGoal(goal4);
    ArrayList<TestCase> testCases = new ArrayList<>();
    testCases.add(test1);
    testCases.add(test2);
    testCases.add(test3);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
    String nameTest1 = naming.getName(test1);
    String nameTest2 = naming.getName(test2);
    String nameTest3 = naming.getName(test3);
    assertEquals("Generated test name differs from expected", "testMist", nameTest1);
    assertEquals("Generated test name differs from expected", "testKeysReturningPositive", nameTest2);
    assertEquals("Generated test name differs from expected", "testKeysReturningNegative", nameTest3);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 35 with TestFitnessFunction

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

the class TestMethodNamingComplexExamples method testTwoTestsOutputGoals.

@Test
public void testTwoTestsOutputGoals() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "keys()[I");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
    TestFitnessFunction goal3 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "keys()", Type.INT_TYPE, NUM_NEGATIVE));
    TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "keys()", Type.INT_TYPE, NUM_POSITIVE));
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal2);
    test1.addCoveredGoal(goal3);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal2);
    test2.addCoveredGoal(goal4);
    ArrayList<TestCase> testCases = new ArrayList<>();
    testCases.add(test1);
    testCases.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
    String nameTest1 = naming.getName(test1);
    String nameTest2 = naming.getName(test2);
    // TODO: testKeysReturningNegative?
    assertEquals("Generated test name differs from expected", "testKeysTakingNoArgumentsReturningNegative", nameTest1);
    // TODO: testKeysReturningPositive?
    assertEquals("Generated test name differs from expected", "testKeysTakingNoArgumentsReturningPositive", nameTest2);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) 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