简体   繁体   中英

How to see test-by-test stdout/stderr in Maven surefire

I run a JUnit-based test suite in Maven, some 200 tests in total. Many produce Log4J DEBUG or ERROR lines, and I see them all mashed into a single long file in my surefire-reports directory.

I want to see all this output divided up by test, so I can discover what test produced what output. How can I do this?

The most straightforward solution would be to a call to the following method to the beginning and end of each test:

static void printBeginning(PrintStream stream) {
  String methodName = Thread.currentThread().getStackTrace()[0].getMethodName();
  stream.format("---- Beginning Test: %s ----%n", methodName);
}

However, if you want to use surefire, the way to do this would be to write your own custom RunListener :

Basically, assign the RunListener in Maven, as in this documentation

<plugins>
[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
      <properties>
        <property>
          <name>listener</name>
          <value>com.mycompany.MyResultListener,com.mycompany.MyResultListener2</value>
        </property>
    </configuration>
  </plugin>
[...]
</plugins>

Then the code will be run by Surefire.

You can read more about how to do this here: Using JUnit RunListener with Maven

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM