Python unittest - 使用 setupclass 和多个设置/删除?

Python unittest - using setupclass, and more than one setup / takedown?

我了解到 setUpClass/tearDownClass 方法在 class 中被调用一次,并且 setUp()/tearDown() 在每个单元测试中被调用 before/after。有关此标准设置,请参阅下面的代码片段:

class Example(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        do_expensive_setup_of_immutable_objects_used_by_all_tests()

    def setUp(self):
        do_setup_before_each_test()

    def test1(self):
        do_test1_stuff()

    def test2(self):
        do_test1_stuff()

    def tearDown(self):
        do_teardown_after_each_test()

    @classmethod
    def tearDownClass(cls):
        do_teardown_of_immutable_objects_used_from_all_tests()

但是,我希望某些单元测试具有与其他单元测试不同的 setUp()tearDown()。什么被认为是实现此功能的良好测试结构?我有什么选择?我在 Python 方面没有经验,想知道编写涉及昂贵 setUp() 程序的单元测试的好风格。

也许这就是@Klaus 的意思?

import unittest


class TestA(unittest.TestCase):
    def setUp(self):
        print "Setup # 1"

    def test1(self):
        print "Doing test1"

class TestB(unittest.TestCase):
    def setUp(self):
        print "Setup # 2"

    def test2(self):
        print "Doing test2"


if __name__ == "__main__":
    unittest.main()

或者,您可以根据方法名称更改设置。
尽管维护起来可能会更痛苦一些。

class TestA(unittest.TestCase):
    def setUp(self):
        if self._testMethodName == "test1":
            print "Setup # 1"
        elif self._testMethodName == "test2":
            print "Setup # 2"

    def test1(self):
        print "Doing test1"

    def test2(self):
        print "Doing test2"


if __name__ == "__main__":
    unittest.main()

两者产生相同的输出:

Output:
Setup # 1
Doing test1
.Setup # 2
Doing test2
.
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

编辑: 抱歉,我认为我没有完全理解,但这是我尝试执行您的评论的尝试。 我的方法是重新应用 BaseTest.setUp(),而不是像您描述的那样保持原样。
TypeOneTestsTypeTwoTests 继承 BaseTest 并且他们使用 super(...)

在他们自己的内部调用 BaseTestsetUp 函数
class BaseTest(unittest.TestCase):
    def setUp(self):
        print "BaseTest setUp"

    def tearDown(self):
        print "BaseTest tearDown"

    # you could add tests here but I didn't add any based on your comment


class TypeOneTests(BaseTest):
    def setUp(self):
        super(TypeOneTests, self).setUp()
        print "TypeOneTests setUp"

    def tearDown(self):
        print "TypeOneTests tearDown"

    def test(self):
        print "a test in TypeOneTests"


class TypeTwoTests(BaseTest):
    def setUp(self):
        super(TypeTwoTests, self).setUp()
        print "TypeTwoTests additional setUp"

    def tearDown(self):
        print "TypeTwoTests tearDown"
        super(TypeTwoTests, self).tearDown()

    def test(self):
        print "a test in TypeTwoTests"


if __name__ == "__main__":
    unittest.main()

输出:

BaseTest setUp
TypeOneTests setUp
a test in TypeOneTests
TypeOneTests tearDown
.BaseTest setUp
TypeTwoTests additional setUp
a test in TypeTwoTests
TypeTwoTests tearDown
BaseTest tearDown
.
----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK