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

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屏蔽掉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值