use of org.evosuite.testcarver.testcase.CarvedTestCase 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.testcarver.testcase.CarvedTestCase in project evosuite by EvoSuite.
the class CarvingRunListener method processLog.
/**
* Creates TestCase out of the captured log
*
* @param description
* @param log
* log captured from test execution
*/
private void processLog(Description description, final CaptureLog log) {
final CaptureLogAnalyzer analyzer = new CaptureLogAnalyzer();
final EvoTestCaseCodeGenerator codeGen = new EvoTestCaseCodeGenerator();
logger.debug("Current log: " + log);
List<Class<?>> observedClasses = getObservedClasses(log);
for (Class<?> targetClass : observedClasses) {
logger.debug("Current observed class: {}", targetClass.getName());
Class<?>[] targetClasses = new Class<?>[1];
targetClasses[0] = targetClass;
if (!carvedTests.containsKey(targetClass))
carvedTests.put(targetClass, new ArrayList<TestCase>());
analyzer.analyze(log, codeGen, targetClasses);
CarvedTestCase test = (CarvedTestCase) codeGen.getCode();
if (test == null) {
logger.info("Failed to carve test for " + Arrays.asList(targetClasses));
codeGen.clear();
continue;
}
test.setName(description.getMethodName());
logger.info("Carved test of length " + test.size());
try {
test.changeClassLoader(TestGenerationContext.getInstance().getClassLoaderForSUT());
GenericTypeInference inference = new GenericTypeInference();
// test.accept(inference);
inference.inferTypes(test);
carvedTests.get(targetClass).add(test);
} catch (Throwable t) {
logger.info("Exception during carving: " + t);
for (StackTraceElement elem : t.getStackTrace()) {
logger.info(elem.toString());
}
logger.info(test.toCode());
}
codeGen.clear();
}
}
use of org.evosuite.testcarver.testcase.CarvedTestCase in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testCarvedTestNames.
@Test
public void testCarvedTestNames() {
Properties.TARGET_CLASS = MethodWithSeveralInputArguments.class.getCanonicalName();
;
Properties.SELECTED_JUNIT = TestMethodWithSeveralInputArguments.class.getCanonicalName();
;
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertEquals("Incorrect number of carved tests", 2, factory.getNumCarvedTestCases());
CarvedTestCase tc1 = (CarvedTestCase) factory.getCarvedTestCases().get(0);
Assert.assertEquals("Incorrect carved test name", "testWithNull", tc1.getName());
System.out.println("Carved Test Case # " + tc1.getID() + ": " + tc1.getName());
System.out.println(tc1.toCode());
CarvedTestCase tc2 = (CarvedTestCase) factory.getCarvedTestCases().get(1);
Assert.assertEquals("Incorrect carved test name", "testWithArray", tc2.getName());
System.out.println("Carved Test Case # " + tc2.getID() + ": " + tc2.getName());
System.out.println(tc2.toCode());
}
use of org.evosuite.testcarver.testcase.CarvedTestCase in project evosuite by EvoSuite.
the class TestSuiteWriterUtils method getNameOfTest.
public static String getNameOfTest(List<TestCase> tests, int position) {
TestCase test = tests.get(position);
String testName = null;
if (test instanceof CarvedTestCase) {
testName = ((CarvedTestCase) test).getName();
} else {
int totalNumberOfTests = tests.size();
String totalNumberOfTestsString = String.valueOf(totalNumberOfTests - 1);
String testNumber = StringUtils.leftPad(String.valueOf(position), totalNumberOfTestsString.length(), "0");
testName = "test" + testNumber;
}
return testName;
}
Aggregations