上面这句是从nose的官方文档中摘出来的,我当时不能理解最后一句话是什么意思,就做了下面这个小实验:
class testgenerator():
def setUp(self):
print "MyTestClass setup"
def tearDown(self):
print "*******************"
def testfunc(self):
print "This is a test function"
def check_odd(self, m, mm):
print "I am the check function"
assert m % 2 == 1 or mm % 2 == 1
def test_odds(self):
for i in range(0, 3):
yield self.check_odd, i, i * 2
最后的结果如下:
MyTestClass setup
I am the check function
*******************
MyTestClass setup
I am the check function
*******************.
MyTestClass setup
I am the check function
*******************
MyTestClass setup
This is a test function
*******************.
结论: setUp and tearDown 在函数test_odds运行之前不会运行,即setUp和tearDown不修饰test_odds函数,
因为test_odds是一个generator method
PS:
运行上述例子的时候使用 nosetests -s
-s 参数使得print 的输出正常显示在屏幕上,不会被nose屏蔽掉