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());
}
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;
}
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));
}
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);
}
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);
}
Aggregations