在nose测试中使用继承setUp()和tearDown()方法

时间:2015-03-09 06:26:36

标签: python python-2.7 nosetests

我的鼻子套装和一些子类中有一个普通的测试类,继承了它。

配置同样如下:

class CGeneral_Test(object)::
    """This class defines the general testcase"""
    def __init__ (self):
        do_some_init()
        print "initialisation of general class done!"

    def setUp(self):
        print "this is the general setup method"
        do_setup()

    def tearDown(self):
        print "this is the general teardown method"
        do_teardown()

现在,我的子类看起来像这样:

class CIPv6_Test(CGeneral_Test):
    """This class defines the sub, inherited testcase"""
    def __init__ (self):
        super(CIPv6_Test, self).__init__()
        do_some_sub_init()
        print "initialisation of sub class done!"

    def setUp(self):
        print "this is the per-test sub setup method"
        do_sub_setup()

    def test_routing_64(self):
        do_actual_testing_scenarios()

    def tearDown(self):
        print "this is the  per-test sub teardown method"
        do_sub_teardown()

所以,我想要实现的是每个测试都会调用子类和超类setUp方法。 因此,所需的测试顺序是:

Base Setup
Inherited Setup
This is a some test.
Inherited Teardown
Base Teardown

当然,这可以通过从继承的CGeneral_Test.setUp(self)方法调用setUp()来实现。

默认情况下是否有任何配置可以实现此行为而无需专门调用super setUp和tearDown方法?

谢谢!

1 个答案:

答案 0 :(得分:3)

不,但您无需指定CGeneral_Test。你没有CIPv6_Test.__init__,你可以在这里使用相同的策略:

class CIPv6_Test(CGeneral_Test):
    def setUp(self):
        super(CIPv6_Test, self).setUp()
        print "this is the per-test sub setup method"
        do_sub_setup()