Python 在每个单元测试用例拆卸之后或之前调用函数

Python 在每个单元测试用例拆卸之后或之前调用函数,python,unit-testing,Python,Unit Testing,我有一个函数removeElements(),需要在每个测试用例的拆卸过程中调用它。有什么方法可以在拆卸时自动调用此函数,而不是在每个测试用例中复制函数调用?Python有一种方法可以使用: import unittest class ElementTestCase(unittest.TestCase): def setUp(self): addElements() def tearDown(self): removeElements()

我有一个函数removeElements(),需要在每个测试用例的拆卸过程中调用它。有什么方法可以在拆卸时自动调用此函数,而不是在每个测试用例中复制函数调用?

Python有一种方法可以使用:

import unittest

class ElementTestCase(unittest.TestCase):
    def setUp(self):
        addElements()

    def tearDown(self):
        removeElements()

    #define test cases...
Python提供了一种可以使用的方法:

import unittest

class ElementTestCase(unittest.TestCase):
    def setUp(self):
        addElements()

    def tearDown(self):
        removeElements()

    #define test cases...

似乎您已经了解了
testCase
方法,并且已经知道如何继承实现,唯一缺少的是如何继承实现,但仍然允许自定义

最简单的方法是创建一个中间类或mixin类来实现
tearDown
hook。例如,假设您正在编写这样的测试用例:

class MyTestCase(unittest.TestCase):
    def tearDown(self):
        print('Tearing down {}'.format(self))
…但您希望它们都调用
removeements

定义此中间类:

class ElementRemovingTestCase(unittest.TestCase):
    def tearDown(self):
        self.removeElements()
现在用这种方式写你的案例:

class MyTestCase(ElementRemovingTestCase):
    def tearDown(self):
        super(MyTestCase, self).tearDown()
        print('Tearing down {}'.format(self))
如果您不想向上传递
tearDown
方法解析链,以便在每个测试用例中保存一行代码,您可以只定义一个新的钩子协议:

class ElementRemovingTestCase(unittest.TestCase):
    def tearDown(self):
        self.removeElements()
        self.additionalTearDown()

class MyTestCase(ElementRemovingTestCase):
    def additionalTearDown(self):
        print('Tearing down {}'.format(self))
Python中OO的任何其他常用选项都可以在这里使用,无论您多么疯狂:

for name, case in inspect.getmembers(sys.modules[__name__], 
                                     lambda cls: issubclass(cls, unittest.TestCase)):
    real_tearDown = case.tearDown
    def tearDown(self):
        self.removeElements()
        real_tearDown(self)
    case.tearDown = real_tearDown

似乎您已经了解了
testCase
方法,并且已经知道如何继承实现,唯一缺少的是如何继承实现,但仍然允许自定义

最简单的方法是创建一个中间类或mixin类来实现
tearDown
hook。例如,假设您正在编写这样的测试用例:

class MyTestCase(unittest.TestCase):
    def tearDown(self):
        print('Tearing down {}'.format(self))
…但您希望它们都调用
removeements

定义此中间类:

class ElementRemovingTestCase(unittest.TestCase):
    def tearDown(self):
        self.removeElements()
现在用这种方式写你的案例:

class MyTestCase(ElementRemovingTestCase):
    def tearDown(self):
        super(MyTestCase, self).tearDown()
        print('Tearing down {}'.format(self))
如果您不想向上传递
tearDown
方法解析链,以便在每个测试用例中保存一行代码,您可以只定义一个新的钩子协议:

class ElementRemovingTestCase(unittest.TestCase):
    def tearDown(self):
        self.removeElements()
        self.additionalTearDown()

class MyTestCase(ElementRemovingTestCase):
    def additionalTearDown(self):
        print('Tearing down {}'.format(self))
Python中OO的任何其他常用选项都可以在这里使用,无论您多么疯狂:

for name, case in inspect.getmembers(sys.modules[__name__], 
                                     lambda cls: issubclass(cls, unittest.TestCase)):
    real_tearDown = case.tearDown
    def tearDown(self):
        self.removeElements()
        real_tearDown(self)
    case.tearDown = real_tearDown

您正在使用哪个测试模块?在不知道的情况下很难回答…我使用的是unittest.TestCase您使用的是哪个测试模块?在不知道的情况下很难回答…我正在使用unittest.testcase谢谢Michael,但是假设在所有测试用例中有100个测试用例我需要调用removeElements。想知道有没有其他方法可以让teardown内部调用自己删除元素。unittest为每个测试运行自动调用setUp和teardown。在上面的unittest链接中:“当定义setUp()方法时,测试运行程序将在每次测试之前运行该方法。同样,如果定义了tearDown()方法,测试运行程序将在每次测试之后调用该方法。”谢谢Michael,但假设在所有测试用例中有100个测试用例我需要调用removeElements。想知道有没有其他方法可以让teardown内部调用自己删除元素。unittest为每个测试运行自动调用setUp和teardown。从上面的unittest链接:“定义setUp()方法时,测试运行程序将在每次测试之前运行该方法。同样,如果定义了tearDown()方法,测试运行程序将在每次测试之后调用该方法”