Time will tell.
续上章内容,本章节继续来说说 UnitTest 框架对于测试结果的处理方式。
从结构上来看,TestResult 就是一个单独的结果类,所有的逻辑全靠 TestCase 来做调度。
一、引入结果
在 case 的最上方,结果函数被直接引入:
from . import result
在 TestCase 中的默认结果函数,实例化了这个结果类的对象。
def defaultTestResult(self):
return result.TestResult()
二、实例化结果对象
上文说过,TestCase 执行的时候,调用的是 run 方法,这个方法有一个result 的参数,默认是 None,一般来说,不做二次开发,使用的都是默认的参数。
if result is None:
result = self.defaultTestResult()
三、执行用例的结果记录
执行的时候调用了 startTestRun() 方法。这里有个疑问点,这个方法在 result 中其实是一个空方法。
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
startTestRun()
在开始执行用例前调用如下方法:
result.startTest(self)
def startTest(self, test):