您的位置:首页 > 其它

Pyunit源码笔记之六 testcase run的执行前奏

2017-06-01 14:09 633 查看
接上文,

for index, test in enumerate(self)第三次,test就是testcase:

testmul (__main__.MyTest)

testsum (__main__.MyTest)

TestCase.py中的class TestCase(object)的__call__()

def __call__(self, *args, **kwds):
return self.run(*args, **kwds)
TestCase.py中的class TestCase(object)的def run(self, result=None)

def run(self, result=None):
......

result.startTest(self)

testMethod = getattr(self, self._testMethodName)
if (getattr(self.__class__, "__unittest_skip__", False) or
getattr(testMethod, "__unittest_skip__", False)):
# If the class or method was skipped.
try:
skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
or getattr(testMethod, '__unittest_skip_why__', ''))
self._addSkip(result, self, skip_why)
finally:
result.stopTest(self)
return
expecting_failure_method = getattr(testMethod,
"__unittest_expecting_failure__", False)
expecting_failure_class = getattr(self,
"__unittest_expecting_failure__", False)
expecting_failure = expecting_failure_class or expecting_failure_method
4000
outcome = _Outcome(result)
try:
self._outcome = outcome


result.startTest(self)执行,也调用result.py的def startTest(self, test),testcase计数self.testsRun += 1

def startTest(self, test):
"Called when the given test is about to be run"
self.testsRun += 1
self._mirrorOutput = False
self._setupStdout()
testMethod = getattr(self, self._testMethodName)取得 测试用例方法:

<bound method MyTest.testmul of <__main__.MyTest testMethod=testmul>>

<bound method MyTest.testsum of <__main__.MyTest testMethod=testsum>>

self._addSkip(result, self, skip_why)向result中添加skiped cases的信息,也调用result.py的addSkip(self, test, reason),self.skipped是一个list。

def addSkip(self, test, reason):
"""Called when a test is skipped."""
self.skipped.append((test, reason))


下面用class _Outcome(object)来执行和保存执行中的信息

class _Outcome(object):
def __init__(self, result=None):
self.expecting_failure = False
self.result = result
self.result_supports_subtests = hasattr(result, "addSubTest")
self.success = True
self.skipped = []
self.expectedFailure = None
self.errors = []
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: