免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2809 | 回复: 0
打印 上一主题 下一主题

unittest.TestCase的__init__为什么不是1个case调1次而是集中调用? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-05-19 14:31 |只看该作者 |倒序浏览
本帖最后由 zhycjh 于 2018-05-19 14:33 编辑

我有下面的一小段测试代码,声明了一个unittest.TestCase的继承类:

import unittest
class mytest(unittest.TestCase):
    def __init__(self,methodName='runTest'):
        super(mytest,self).__init__(methodName)
        print '__init__'
    def setUp(self):
        print '----setUp'
    def tearDown(self):
        print '----tearDown'
    def testTrue1(self):
        print "true1 begin"
        self.assertTrue(True)
        print "true1 end"
    def testTrue2(self):
        print "true2 begin"
        self.assertTrue(True)
        print "true2 end"
if __name__=="__main__":
    unittest.main()

代码输出:

__init__
__init__
----setUp
true1 begin
true1 end
----tearDown
.----setUp
true2 begin
true2 end
----tearDown
.
----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK



1. 不是每个case都调用一次__init__然后setUp,而是先__init__了两次。这是为什么?
2. 为什么每次tearDown结束之后,会另起一行并且打印一个"."号?

谢谢


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP