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

cyb*_*101 1 python nosetests 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()
Run Code Online (Sandbox Code Playgroud)

现在,我的子类如下所示:

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()
Run Code Online (Sandbox Code Playgroud)

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

Base Setup
Inherited Setup
This is a some test.
Inherited Teardown
Base Teardown
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢!

nel*_*fin 5

不,但您无需指定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()
Run Code Online (Sandbox Code Playgroud)