对于nose框架中class级别的setUp和tearDown函数的一点理解

来源:互联网 发布:淘宝联盟省钱购买 编辑:程序博客网 时间:2024/05/22 07:03

For generator methods, the setUp and tearDown methods of the class (if any) will be run before and after each generated test case. The setUp and tearDown methods do not run before the generator method itself, as this would cause setUp to run twice before the first test without an intervening tearDown.


上面这句是从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屏蔽掉

原创粉丝点击