多个测试模块的设置和拆卸方法

时间:2017-12-01 19:56:24

标签: python testing automated-tests python-unittest

我有许多测试,分散在不同的测试模块和类中。我需要在运行测试之前/之后启动/拆除docker容器,但The method add(enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (String) The method add(int, enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (int) The method add(int, enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (int) The method add(enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (String) The method add(enrollment) in the type LinkedList<enrollment> is not applicable for the arguments (String) at enrollment.main(enrollment.java:44) setUpClass()似乎都不合适。如何为多个模块指定设置和拆卸方法?

1 个答案:

答案 0 :(得分:0)

为您的所有测试创建一个基类,您可以在其中放置常用的setUpClasstearDownClass方法:

class MyTestCase(unittest.TestCase):
    def setUpClass(self):
        ...set up docker stuff...

    def tearDownClass(self):
        ...tear down docker stuff...

现在让所有测试类继承自该基类:

from mymodule.mytestcase import MyTestCase

class TestMyNiftyFeature(MyTestCase):
    def test_feature(self):
        ...test things here...

这将在每个类之前和之后运行setup / teardown方法,这通常是你想要的,因为你希望你的测试是自包含的,这样你就可以在一个文件中运行测试而不在另一个文件中运行测试。