【问题标题】:maven surefire: how to print current test being run?maven surefire:如何打印当前正在运行的测试?
【发布时间】:2015-06-01 19:39:58
【问题描述】:

有没有办法让 maven surefire 打印它开始的每个单元测试(即测试方法)的名称?

类似:

testFoo: ... passed
testBar: ... failed

【问题讨论】:

标签: java maven maven-surefire-plugin


【解决方案1】:

详细说明

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()
                );
    }
}

在 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>

【讨论】:

  • 似乎System.out.println 没有进入 Maven 控制台日志,也没有任何报告。
【解决方案2】:

这有点牵强,但您可以实现RunListener 并将其添加到surefire。看看如何配置它here

【讨论】:

  • 相反,为什么不从测试代码本身记录测试名称?
【解决方案3】:

或者,您可以使用 --debug-X 标志在调试模式下运行 maven

【讨论】:

  • 它不记录成功执行的测试。
猜你喜欢
  • 2016-04-26
  • 1970-01-01
  • 2023-03-09
  • 2020-12-31
  • 2020-05-08
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
相关资源
最近更新 更多