JUnit Ant task won't output to screen

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.

  • 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
    

    Stéphane, the lack of unit test output is probably linked to the presence of this hard-coded subprocess stream handler in JUnitTask.executeAsForked() (see the source code of ant-junit):

        Execute execute = new Execute(
            new JUnitLogStreamHandler(
                this,
                Project.MSG_INFO,
                Project.MSG_WARN),
            watchdog);
    

    The method is private and I don't see an obvious "official" way to pass another stream handler, so if you really want to handle the forked JVM's output differently you can probably take advantage of the fact that ant-junit is open source and create a slightly altered version of it that either uses more verbose settings for the JUnitLogStreamHandler, or even uses your own implementation of ExecuteStreamHandler that handles the output of the child process however you prefer.

    链接地址: http://www.djcxy.com/p/15102.html

    上一篇: Haskell:实例中的非法类型同义词族应用程序

    下一篇: JUnit Ant任务不会输出到屏幕