Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit Ant task won't output to screen

Tags:

java

junit

ant

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.
like image 347
Stéphane Bruckert Avatar asked Aug 08 '13 16:08

Stéphane Bruckert


1 Answers

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
like image 109
Delildor Avatar answered Oct 16 '22 06:10

Delildor