unittest.TestCase的__init__为什么不是1个case调1次而是集中调用?

weixin_42195275 2018-05-19 12:40:49
我有下面的一小段测试代码,声明了一个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()

上面我有两个case,testTrue1和testTrue2。我期待的ut行为是,对于每个case而言:
1. __init__
2. setUp
3. case本身
4. tearDown

但是这个代码的输出是:

__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结束之后,会另起一行并且打印一个"."号?

谢谢
...全文
749 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧