Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven surefire: how to print current test being run?

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
like image 507
Gal Avatar asked Jun 01 '15 19:06

Gal


People also ask

Where does Maven store test results?

By default, these files are generated at ${basedir}/target/surefire-reports . As such, after the tests are executed, you just need to open those reports in Eclipse by double-clicking them.

What does mvn test command do?

mvn test-compile: Compiles the test source code. mvn test: Runs tests for the project. mvn package: Creates JAR or WAR file for the project to convert it into a distributable format. mvn install: Deploys the packaged JAR/ WAR file to the local repository.


2 Answers

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>
like image 90
Paul Verest Avatar answered Sep 19 '22 19:09

Paul Verest


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

like image 36
Robert Avatar answered Sep 22 '22 19:09

Robert