对于鼻子测试类,使用__init __(self)而不是setup(self)有不利之处吗? [英] Is there a downside for using __init__(self) instead of setup(self) for a nose test class?
问题描述
为
运行nosetests -s
class TestTemp():
def __init__(self):
print '__init__'
self.even = 0
def setup(self):
print '__setup__'
self.odd = 1
def test_even(self):
print 'test_even'
even_number = 10
assert even_number % 2 == self.even
def test_odd(self):
print 'test_odd'
odd_number = 11
assert odd_number % 2 == self.odd
打印出以下内容.
__init__
__init__
__setup__
test_even
.__setup__
test_odd
.
在运行测试之前创建测试实例,而在测试之前立即运行安装程序.
The test instances are created before tests are run, while setup runs right before the test.
在一般情况下,__init__()
和setup()可以完成相同的操作,但是使用__init__()
代替setup()有不利之处吗?还是同时使用两者?
For the general case, __init__()
and setup() accomplish the same thing, but is there a downside for using __init__()
instead of setup()? Or using both?
推荐答案
虽然__init__
可以代替setUp
,但是您应该坚持使用setUp
,因为它是编写测试的风格化协议的一部分.它还有一个tearDown
对应物,而__init__
没有,以及__init__
没有的类级和模块级的对应物.
While __init__
may work as a replacement for setUp
, you should stick to setUp
because it is part of the stylized protocol for writing tests. It also has a counterpart, tearDown
, which __init__
does not, as well as class- and module-level counterparts which __init__
does not.
编写测试类与编写普通类不同,因此您应该坚持编写测试类所使用的样式.
Writing test classes is different than writing normal classes, so you should stick to the style used to write test classes.
这篇关于对于鼻子测试类,使用__init __(self)而不是setup(self)有不利之处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!