use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class BIMutatedMethodSeedingTestSuiteChromosomeFactory method getChromosome.
/*
* (non-Javadoc)
*
* @see org.evosuite.ga.ChromosomeFactory#getChromosome()
*/
/**
* {@inheritDoc}
*/
@Override
public TestSuiteChromosome getChromosome() {
/*
* double P_delta = 0.1d; double P_clone = 0.1d; int MAX_CHANGES = 10;
*/
TestSuiteChromosome chromosome = defaultFactory.getChromosome();
int numTests = chromosome.getTests().size();
// reduce seed probablility by number of tests to be generated
final double SEED_CHANCE = Properties.SEED_PROBABILITY / numTests;
for (int i = 0; i < numTests; i++) {
if (bestIndividual != null && Randomness.nextDouble() < SEED_CHANCE && Properties.SEED_MUTATIONS > 0) {
TestSuiteChromosome bi = bestIndividual.clone();
int mutations = Randomness.nextInt(Properties.SEED_MUTATIONS) + 1;
for (int j = 0; j < mutations; j++) {
bi.mutate();
}
int testSize = bi.getTests().size();
TestCase test = bi.getTests().get(Random.nextInt(testSize));
if (test != null) {
List<TestCase> tests = chromosome.getTests();
tests.remove(i);
tests.add(i, test);
chromosome.clearTests();
for (TestCase t : tests) {
chromosome.addTest(t);
}
}
}
}
return chromosome;
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class CarvingManager method readTestCases.
private void readTestCases() throws IllegalStateException {
ClientServices.getInstance().getClientNode().changeState(ClientState.CARVING);
Collection<String> junitTestNames = getListOfJUnitClassNames();
LoggingUtils.getEvoLogger().info("* Executing tests from {} test {} for carving", junitTestNames.size(), junitTestNames.size() == 1 ? "class" : "classes");
final JUnitCore runner = new JUnitCore();
final CarvingRunListener listener = new CarvingRunListener();
runner.addListener(listener);
final List<Class<?>> junitTestClasses = new ArrayList<Class<?>>();
final org.evosuite.testcarver.extraction.CarvingClassLoader classLoader = new org.evosuite.testcarver.extraction.CarvingClassLoader();
// TODO: This really needs to be done in a nicer way!
FieldRegistry.carvingClassLoader = classLoader;
try {
// instrument target class
classLoader.loadClass(Properties.TARGET_CLASS);
} catch (final ClassNotFoundException e) {
throw new RuntimeException(e);
}
for (String className : junitTestNames) {
String classNameWithDots = ResourceList.getClassNameFromResourcePath(className);
try {
final Class<?> junitClass = classLoader.loadClass(classNameWithDots);
junitTestClasses.add(junitClass);
} catch (ClassNotFoundException e) {
logger.error("Failed to load JUnit test class {}: {}", classNameWithDots, e);
}
}
final Class<?>[] classes = new Class<?>[junitTestClasses.size()];
junitTestClasses.toArray(classes);
Result result = runner.run(classes);
logger.info("Result: {}/{}", result.getFailureCount(), result.getRunCount());
for (Failure failure : result.getFailures()) {
logger.info("Failure: {}", failure.getMessage());
logger.info("Exception: {}", failure.getException());
}
Map<Class<?>, List<TestCase>> testMap = listener.getTestCases();
for (Class<?> targetClass : testMap.keySet()) {
List<TestCase> processedTests = new ArrayList<>();
for (TestCase test : testMap.get(targetClass)) {
String testName = ((CarvedTestCase) test).getName();
if (test.isEmpty())
continue;
ExecutionResult executionResult = null;
try {
executionResult = TestCaseExecutor.runTest(test);
} catch (Throwable t) {
logger.info("Error while executing carved test {}: {}", testName, t);
continue;
}
if (executionResult.noThrownExceptions()) {
logger.info("Adding carved test without exception");
logger.info(test.toCode());
processedTests.add(test);
} else {
logger.info("Exception thrown in carved test {}: {}", testName, executionResult.getExceptionThrownAtPosition(executionResult.getFirstPositionOfThrownException()));
for (StackTraceElement elem : executionResult.getExceptionThrownAtPosition(executionResult.getFirstPositionOfThrownException()).getStackTrace()) {
logger.info(elem.toString());
}
logger.info(test.toCode(executionResult.exposeExceptionMapping()));
if (Properties.CHOP_CARVED_EXCEPTIONS) {
logger.info("Chopping exception of carved test");
chopException(test, executionResult);
if (test.hasObject(Properties.getTargetClassAndDontInitialise(), test.size())) {
processedTests.add(test);
} else {
logger.info("Chopped test is empty");
}
} else {
logger.info("Not adding carved test with exception: ");
}
}
}
if (processedTests.size() > 0) {
LoggingUtils.getEvoLogger().info(" -> Carved {} tests for class {} from existing JUnit tests", processedTests.size(), targetClass);
if (logger.isDebugEnabled()) {
for (TestCase test : processedTests) {
logger.debug("Carved Test {}: {}", ((CarvedTestCase) test).getName(), test.toCode());
}
}
} else {
// String outcome = "";
// for (Failure failure : result.getFailures()) {
// outcome += "(" + failure.getDescription() + ", " + failure.getTrace()
// + ") ";
// }
logger.info("It was not possible to carve any test case for class {} from {}", targetClass.getName(), Arrays.toString(junitTestNames.toArray()));
// + ". Test execution results: " + outcome);
}
carvedTests.put(targetClass, processedTests);
}
carvingDone = true;
// TODO: Argh.
FieldRegistry.carvingClassLoader = null;
// TODO:
// ClientNodeLocal client = ClientServices.getInstance().getClientNode();
// client.trackOutputVariable(RuntimeVariable.CarvedTests, totalNumberOfTestsCarved);
// client.trackOutputVariable(RuntimeVariable.CarvedCoverage,carvedCoverage);
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestCoverageGoalNameGeneration method testConstructorExceptionWithFullyQualifiedClassName.
@Test
public void testConstructorExceptionWithFullyQualifiedClassName() {
TestCase test = new DefaultTestCase();
ExceptionCoverageTestFitness goal = new ExceptionCoverageTestFitness("org.package.name.FooClass", "<init>()", RuntimeException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT);
test.addCoveredGoal(goal);
List<TestCase> tests = new ArrayList<>();
tests.add(test);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
String generatedName = naming.getName(test);
assertEquals("testFailsToCreateFooClassThrowsRuntimeException", generatedName);
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestCoverageGoalNameGeneration method testTwoTestsUniqueMethods.
@Test
public void testTwoTestsUniqueMethods() {
TestCase test1 = new DefaultTestCase();
MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString");
test1.addCoveredGoal(goal1);
TestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 0));
MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "getSomeStuff");
test2.addCoveredGoal(goal2);
List<TestCase> tests = new ArrayList<>();
tests.add(test1);
tests.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
assertEquals("testToString", naming.getName(test1));
assertEquals("testGetSomeStuff", naming.getName(test2));
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestCoverageGoalNameGeneration method testTwoInputGoals.
@Test
public void testTwoInputGoals() {
TestCase test = new DefaultTestCase();
InputCoverageGoal inputGoal1 = new InputCoverageGoal("FooClass", "foo", 0, objectType(), REF_NONNULL);
InputCoverageTestFitness goal1 = new InputCoverageTestFitness(inputGoal1);
InputCoverageGoal inputGoal2 = new InputCoverageGoal("FooClass", "foo", 1, objectType(), REF_NULL);
InputCoverageTestFitness goal2 = new InputCoverageTestFitness(inputGoal2);
test.addCoveredGoal(goal1);
test.addCoveredGoal(goal2);
List<TestCase> tests = new ArrayList<>();
tests.add(test);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
String generatedName = naming.getName(test);
assertEquals("testFooWithNonNullAndNull", generatedName);
}
Aggregations