概念
testRunner : 運行測試類的類
@Runwith : 告訴junit的框架應該是使用哪個testRunner
testRunner類關(guān)系
自定義一個簡單的testRunner的方法是繼承junit默認的runner BlockJUnit4ClassRunner腌歉。
public class BlockJUnit4ClassRunner extends ParentRunner<FrameworkMethod> {
@Override
protected List<FrameworkMethod> getChildren() {
// scan test class for methonds annotated with @Test
}
@Override
protected Description describeChild(FrameworkMethod method) {
// create Description based on method name
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
if (/* method not annotated with @Ignore */) {
// run methods annotated with @Before
// run test method
// run methods annotated with @After
}
}
}
簡單的自定義:在沒給測試之前打印日志页慷。
public class MyTestRunner extends BlockJUnit4ClassRunner {
/**
* Creates a BlockJUnit4ClassRunner to run {@code klass}
*
* @param klass
* @throws InitializationError if the test class is malformed.
*/
public MyTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
System.out.println("this is my cusstom test runner : " + method.toString());
super.runChild(method, notifier);
}
}