Context
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.
Issue
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?
Notes:
- these unit tests really need to be forked, it can't be changed,
- just let me know if you need more code, for example the settings of the Ant Project or the Ant Target,
- unit tests indeed contain
Sysouts
, - I've reproduced a consistent
build.xml
file which works, - here is the Apache Ant JUnit repository if needed.