JUnit Ant task not displayed

Context

I use ant1-9-0.jar , ant-junit-1.9.0.jar and ant-launcher-1.9.0.jar to run JUnit programs,

In my code, I have this function that returns a 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; } 

JUnit tests run without problems, and the output is successfully saved in .xml .

Question

I need to print the output to the console because I need real-time results (not only at the end of the whole process). To do this, I 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 does not display logs. I also tried removing formatToFile FormatterElement without success. Do you have any suggestions?

Notes:

  • these unit tests really have to be forked, they cannot be changed,
  • just let me know if you need more code, for example, Ant project settings or Ant purpose ,
  • unit tests do contain Sysouts ,
  • I reproduced the serial build.xml file that works,
  • here is the Apache Ant JUnit repository, if necessary.
+7
java junit ant
source share
2 answers

Stefan, your code for the junit task seems right to handle console output.

I checked the source code of the main ANT class, and you need to define an assembly listener so that it can display the logs.

This is a working example for defining a default listener for logging:

 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 
+3
source share

Stefan, the lack of unit test output is probably due to the presence of this hard-coded subprocess thread handler in JUnitTask.executeAsForked () (see ant -junit source code):

  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 thread handler, so if you really want to handle the forked JVM output in different ways, you can probably use the fact that ant - junit is open source and creates a slightly modified version that either uses more detailed settings for the JUnitLogStreamHandler, or even uses your own implementation of ExecuteStreamHandler, which processes the output of the child process, but you prefer.

0
source share

All Articles