unittest 基础之 —— TestResult

来源:互联网 发布:华为网络交换机价格 编辑:程序博客网 时间:2024/06/07 08:05

testresult就是存储测试结果的,不过通过何种方式调用run函数,最终到Testcase中的run方法时

必须传一个result(如果为None则自己实例化一个TestResult对象)。这个result就是TestResult

对象或者是其子类的对象,我们每次执行的结果都会调用其addFailureaddSuccessaddSkip….

等方法将执行结果保存到TestResult实例属性中。我们还是来看看TestCase的run方法:

def run(self, result=None):        orig_result = result        if result is None:#如果没有传入result对象自己实例化一个TestResult对象            result = self.defaultTestResult()            startTestRun = getattr(result, 'startTestRun', None)            if startTestRun is not None:                startTestRun()        self._resultForDoCleanups = result        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, skip_why)#调用addSkip            finally:                result.stopTest(self)            return        try:            success = False            try:                self.setUp()            except SkipTest as e:                self._addSkip(result, str(e))            except KeyboardInterrupt:                raise            except:                result.addError(self, sys.exc_info())#调用addError            else:                try:                    testMethod()                except KeyboardInterrupt:                    raise                except self.failureException:                    result.addFailure(self, sys.exc_info())#调用addFailure                except _ExpectedFailure as e:                    addExpectedFailure = getattr(result, 'addExpectedFailure', None)                    if addExpectedFailure is not None:                        addExpectedFailure(self, e.exc_info)#调用addExpectedFailure                    else:                        warnings.warn("TestResult has no addExpectedFailure method, reporting as passes",                                      RuntimeWarning)                        result.addSuccess(self)#调用addSuccess                except _UnexpectedSuccess:                    addUnexpectedSuccess = getattr(result, 'addUnexpectedSuccess', None)                    if addUnexpectedSuccess is not None:                        addUnexpectedSuccess(self)                    else:                        warnings.warn("TestResult has no addUnexpectedSuccess method, reporting as failures",                                      RuntimeWarning)                        result.addFailure(self, sys.exc_info())                except SkipTest as e:                    self._addSkip(result, str(e))                except:                    result.addError(self, sys.exc_info())                else:                    success = True                try:                    self.tearDown()                except KeyboardInterrupt:                    raise                except:                    result.addError(self, sys.exc_info())                    success = False            cleanUpSuccess = self.doCleanups()            success = success and cleanUpSuccess            if success:                result.addSuccess(self)        finally:            result.stopTest(self)            if orig_result is None:                stopTestRun = getattr(result, 'stopTestRun', None)                if stopTestRun is not None:                    stopTestRun()
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝呛奶怎么办伴呼噜 离婚了欠的债务怎么办 小鸟生长发育过程壳破了怎么办 经常嗓子发炎抵抗力低下怎么办 生完孩子出虚汗怎么办 3岁宝宝抵抗力差怎么办 5岁宝宝免疫力低怎么办 狗狗后腿o型怎么办 狗狗前腿内八字怎么办 小猫腿摔瘸了怎么办会好吗 小狗的腿断了怎么办 狗后腿摔瘸了怎么办 狗狗脖子挠破了怎么办 狗狗脖子挠破化脓怎么办 狗狗大腿骨折了怎么办啊 狗狗洗澡后拉稀怎么办 狗狗好像生病了怎么办 奶狗前腿断了怎么办 狗摔了一下瘸了怎么办 小狗腿突然瘸了怎么办 狗腿突然瘸了怎么办 狗腿受伤了肿了怎么办 狗狗缺钙腿变形怎么办 小狗腿摔骨折了怎么办 狗狗脚掌被压了怎么办 狗狗的脚骨折了怎么办 给猫灌药水呛到怎么办 吃佐匹克隆白天工作量降低怎么办? 手机网页不显示图片怎么办啊 页眉页脚同前节怎么办 小米8后盖缝隙大怎么办 狗子生了一个不动的小狗怎么办 狗狗肚子有脓包怎么办 小孩幼儿园数学不开窍怎么办 老百姓打仗了报警派出所不管怎么办 和人打架报案了怎么办 皇上死后的妃子怎么办 武警改制警卫系的学员怎么办 正团病故后住房怎么办 遇到保姆式领导该怎么办 限购房子卖不了怎么办