前言
JUnit框架通过几个接口就实现了一个灵活的测试框架,借研究Spring测试帮助类之机,重温一下。
主要接口
Test
描述测试的接口,核心业务方法run(TestResult result)方法用来运行测试,并收集测试结果。
TestCase
一个TestCase可以用来定义多个测试,框架支持两种方式运行一个测试,静态方式下,可以通过重写runTest方法,定义需要激活的测试方法,示例如下:
- TestCase test= new MathTest("add") {
- public void runTest() {
- testAdd();
- }
- };
- test.run();
动态方式使用反射实现runTest,缺省实现要求在构建TestCase时指定需要测试的方法名称,示例如下:
- TestCase test= new MathTest("testAdd");
- test.run();
其run(TestResult result)方法会调用TestResult的run(Test test)方法,而TestResult的run(Test test)方法在做一些结果收集处理后,反过来再调用TestCase的runBare方法来进行实际的测试方法调用。
runBare是一种模板方法模式的实现,子类可以通过重写setUp,tearDown和runTest方法提供相应的测试逻辑。
TestSuite
一个TestSuite可以包含一个或多个TestCase,这是组合模式的实现。可以使用动态方式定义TestSuite,示例如下:
- TestSuite suite= new TestSuite();
- suite.addTest(new MathTest("testAdd"));
- suite.addTest(new MathTest("testDivideByZero"));
也可以通过构造函数静态定义
- TestSuite suite= new TestSuite(MathTest.class);
两种方式都是通过反射机制,解析传递进来的测试类,将方法前缀为将方法前缀为test的公有方法构建成测试实例,并添加到缓存的集合中,运行时再依次遍历这些测试实例完成测试。
TestSuite的run(TestResult result)方法是运行测试的主要入口。
TestResult
TestResult主要用来收集测试结果,内部使用两个集合区分测试失败和测试错误情况,并包含一个监听器列表。Test的run(TestResult result)方法会将控制权转交给TestResult,TestResult会捕获调用Test的runBare方法中的错误,并在测试启动和结束时通知监听器,如果对测试结果感兴趣,可以实现TestListener接口,这是监听器模式的实现。
TestListener
测试监听器提供几个接口方法,用来对测试结果进行处理,如果对测试结果感兴趣,可以在调用Test的run(TestResult result)方法前,将测试监听器实例注册到TestResult实例。
运行序列图
下面我们通过序列图来看看这几个接口之间是如何交互的,这里以TextRunner为例:
-
TextRunner 会从命令窗口中解析出需要测试的 TestSuite 实例,然后交给 doRun 方法处理。
-
doRun 方法首先构建一个 TestResult 实例。
-
然后调用 TestSuite 的 run(TestResult result) 方法。
-
run(TestResult result) 询问 TestResult 的 shouldStop 方法判断调用者是否已经停止测试。
-
如果 TestResult 的 shouldStop 返回 false, 将会遍历 TestSuite 的包含的每一个 TestCase 实例 , 重复执行如下步骤。
-
调用 TestCase 的 run(TestResult result) 方法。
-
TestCase 的 run(TestResult result) 委托给 TestResult 的 run(Test test) 方法。
-
TestResult 的 run(Test test) 方法会调用监听器的 startTest 方法通知测试开始,然后调用 TestCase 的 runBare 执行真正的测试,最后调用监听器的 endTest 方法通知测试结束。
扩展
TestCase的runBare方法是实际执行测试的方法,该方法缺省构建一个模板方法实现。子类可以通过重写此方法提供自定义的实现,比如Spring的ConditionalTestCase就通过改写此方法提供条件化测试执行逻辑。
JUnit自带的RepeatedTest类使用装饰器模式实现一个可重得执行的测试类扩展。