12

Is there any way to get maven surefire to print the name of every unit test (i.e., test method) it's starting?

Something like:

testFoo: ... passed
testBar: ... failed
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Gal
  • 5,338
  • 5
  • 33
  • 55

3 Answers3

6

In details

package com.example.mavenproject;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;

/**
 * @author Paul Verest
 */
public class PrintOutCurrentTestRunListener extends RunListener {
    @Override
    public void testRunStarted(Description description) throws Exception {
        // TODO all methods return null
        System.out.println("testRunStarted " + description.getClassName() + " " + description.getDisplayName() + " "
                + description.toString());
    }

    public void testStarted(Description description) throws Exception {
        System.out.println("testStarted "
                + description.toString());
    }

    public void testFinished(Description description) throws Exception {
        System.out.println("testFinished "
                + description.toString());
    }

    public void testRunFinished(Result result) throws Exception {
        System.out.println("testRunFinished " + result.toString()
                + " time:"+result.getRunTime()
                +" R"+result.getRunCount()
                +" F"+result.getFailureCount()
                +" I"+result.getIgnoreCount()
                );
    }
}

and in pom.xml

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<!-- -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <properties>
                    <property>
                        <name>listener</name>
                        <value>com.example.mavenproject.PrintOutCurrentTestRunListener</value>
                    </property>
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • 1
    Does not appear as though `System.out.println` makes it into the Maven console log, nor any report. – ThaDon Nov 06 '17 at 15:17
4

It's a bit of stretch, but you could implement a RunListener and add it to surefire. See how to configure it here.

Robert
  • 452
  • 2
  • 12
  • Instead, why not log the test name from the test code itself ? – MasterJoe Jul 01 '20 at 20:40
  • For anyone using JUnit 5, you can now provide a TestExecutionListener : https://junit.org/junit5/docs/current/user-guide/#running-tests-listeners – Adrien H Sep 27 '22 at 12:07
0

Alternatively you can run maven in debug mode with --debug or -X flags