您的位置:首页 > 编程语言 > Python开发

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

2017-09-20 23:45 381 查看
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屏蔽掉
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数 框架 python