RunListener (JUnit API)

Junit


org.junit.runner.notification Class RunListener

java.lang.Object
  extended by org.junit.runner.notification.RunListener

public class RunListener
extends Object

If you need to respond to the events during a test run, extend RunListener and override the appropriate methods. If a listener throws an exception while processing a test event, it will be removed for the remainder of the test run.

For example, suppose you have a Cowbell class that you want to make a noise whenever a test fails. You could write:

 public class RingingListener extends RunListener {
    public void testFailure(Failure failure) {
       Cowbell.ring();
    }
 }
 

To invoke your listener, you need to run your tests through JUnitCore.

 public void main(String... args) {
    JUnitCore core= new JUnitCore();
    core.addListener(new RingingListener());
    core.run(MyTestClass.class);
 }
 

另请参见:
JUnitCore

构造器摘要
RunListener()
           
 
方法摘要
 void testAssumptionFailure(Failure failure)
          Called when an atomic test flags that it assumes a condition that is false
 void testFailure(Failure failure)
          Called when an atomic test fails.
 void testFinished(Description description)
          Called when an atomic test has finished, whether the test succeeds or fails.
 void testIgnored(Description description)
          Called when a test will not be run, generally because a test method is annotated with Ignore.
 void testRunFinished(Result result)
          Called when all tests have finished
 void testRunStarted(Description description)
          Called before any tests have been run.
 void testStarted(Description description)
          Called when an atomic test is about to be started.
 
类方法继承: java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造器详细信息

RunListener

public RunListener()
方法详细信息

testRunStarted

public void testRunStarted(Description description)
                    throws Exception
Called before any tests have been run.

参数:
description - describes the tests to be run
抛出异常:
Exception

testRunFinished

public void testRunFinished(Result result)
                     throws Exception
Called when all tests have finished

参数:
result - the summary of the test run, including all the tests that failed
抛出异常:
Exception

testStarted

public void testStarted(Description description)
                 throws Exception
Called when an atomic test is about to be started.

参数:
description - the description of the test that is about to be run (generally a class and method name)
抛出异常:
Exception

testFinished

public void testFinished(Description description)
                  throws Exception
Called when an atomic test has finished, whether the test succeeds or fails.

参数:
description - the description of the test that just ran
抛出异常:
Exception

testFailure

public void testFailure(Failure failure)
                 throws Exception
Called when an atomic test fails.

参数:
failure - describes the test that failed and the exception that was thrown
抛出异常:
Exception

testAssumptionFailure

public void testAssumptionFailure(Failure failure)
Called when an atomic test flags that it assumes a condition that is false

参数:
failure - describes the test that failed and the AssumptionViolatedException that was thrown

testIgnored

public void testIgnored(Description description)
                 throws Exception
Called when a test will not be run, generally because a test method is annotated with Ignore.

参数:
description - describes the test that will not be run
抛出异常:
Exception