Junit 如何显示当前执行的测试方法的名称

Junit 如何显示当前执行的测试方法的名称,junit,Junit,我有大量的测试,希望(通过maven触发)看到stdout/stderr上的测试,目前正在执行哪个测试方法 我不希望也不需要在测试本身中这样做,而是遵循正在发生的事情——有点像IntelliJ那样,在圆圈中显示红色或绿色 是否有一个命令行选项来执行此操作,或者我必须编写自己的测试运行程序来为我执行此操作?您最好的选项可能是一个,您可以将其插入Maven: RunListener侦听测试事件,如测试开始、测试结束、测试失败、测试成功等 public class RunListener {

我有大量的测试,希望(通过maven触发)看到stdout/stderr上的测试,目前正在执行哪个测试方法

我不希望也不需要在测试本身中这样做,而是遵循正在发生的事情——有点像IntelliJ那样,在圆圈中显示红色或绿色


是否有一个命令行选项来执行此操作,或者我必须编写自己的测试运行程序来为我执行此操作?

您最好的选项可能是一个,您可以将其插入Maven:

RunListener
侦听测试事件,如测试开始、测试结束、测试失败、测试成功等

public class RunListener {
    public void testRunStarted(Description description) throws Exception {}
    public void testRunFinished(Result result) throws Exception {}
    public void testStarted(Description description) throws Exception {}
    public void testFinished(Description description) throws Exception {}
    public void testFailure(Failure failure) throws Exception {}
    public void testAssumptionFailure(Failure failure) {}
    public void testIgnored(Description description) throws Exception {}
}
然后,在surefire中,您可以使用以下命令指定自定义侦听器:

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

org.apache.maven.plugins

Matthew为JUnit和TestNG编写的内容也有类似的接口

我正在使用TestNG,将详细度设置为高对我来说很有用:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
                <properties>
                    <property>
                        <name>surefire.testng.verbose</name>
                        <value>10</value>
                    </property>
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>

org.apache.maven.plugins

详细级别介于0和10之间,其中10是最详细的。 您可以指定-1,这将使TestNG处于调试模式(不再是 切片堆栈跟踪和所有)。默认级别为0

这里也有答案

然后,输出包含测试进度的行:

[TestNG]调用:“Surefire测试”- org.example.MyTest.myMethod(java.lang.String, java.lang.String)(值

还提到了@BeforeClass和@AfterClass方法