I am using ant1-9-0.jar, ant-junit-1.9.0.jar and ant-launcher-1.9.0.jar to run JUnit tests programmatically.
In my code, I have this function that returns the JUnit Task:
/**
* Generates a JUnit task which runs every single test in a new JVM
* @return task The JUnit task
* @throws Exception
*/
public JUnitTask generateRunTestsTask() throws Exception {
/* New JUnit task */
JUnitTask task = new JUnitTask();
task.init();
/* Summary settings */
JUnitTask.SummaryAttribute sa = new JUnitTask.SummaryAttribute();
sa.setValue("withOutAndErr");
task.setPrintsummary(sa);
/* JVM configuration */
task.setFork(true);
task.setDir(new File(this.deliveryBinDir));
task.createJvmarg().setValue("-Duser.dir=" + this.deliveryBinDir);
/* Output to file */
FormatterElement.TypeAttribute typeFile = new FormatterElement.TypeAttribute();
typeFile.setValue("xml");
FormatterElement formatToFile = new FormatterElement();
formatToFile.setType(typeFile);
task.addFormatter(formatToFile);
/* Task options */
task.setHaltonfailure(false);
task.setShowOutput(true);
task.setOutputToFormatters(true);
List<String> testSuites = getTestJarList(this.deliveryLibFolder);
for (String singleSuite : testSuites) {
JUnitTest test = new JUnitTest(singleSuite);
/* Sets reports location */
test.setTodir(this.testReportsFolder);
task.addTest(test);
}
return task;
}
The JUnit tests run without problem and the output is successfully stored into .xml files.
I need to print the output to the console, because I want results in live (not only at the end of the whole process). To do so, I have added a second FormatterElement just below the /** Output to file */ block:
/* Output to screen */
FormatterElement.TypeAttribute typeScreen = new FormatterElement.TypeAttribute();
typeScreen.setValue("plain");
FormatterElement formatToScreen = new FormatterElement();
formatToScreen.setType(typeScreen);
formatToScreen.setUseFile(false);
formatToScreen.setOutput(System.out);
task.addFormatter(formatToScreen);
But my console still doesn't display the logs. I have also tried to remove the formatToFile FormatterElement, without success. Do you have any suggestions?
Sysouts,build.xml file which works,Stéphane, your code for the junit task seems to be correct for handling the output to the console.
I have check the source code of the Main class of ANT and you need to define a build listener to be able to display the logs.
This is working example to define a default listener for logging purpose:
BuildLogger logger = new DefaultLogger();
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.err);
logger.setMessageOutputLevel(Project.MSG_INFO);
logger.setEmacsMode(true);
project.addBuildListener(logger); //add build listener to your define project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With