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

Using inheritance setUp() and tearDown() methods in nose tests

我的鼻子测试套装中有一个通用测试 class 和一些继承自它的子 classes。

配置同样是:

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()

现在,我有子classes,看起来像这样:

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()

所以,我想要实现的是每个测试都将调用 sub-class 和 super class setUp 方法。 因此,所需的测试顺序是:

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

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

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

谢谢!

否,但您无需指定 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()