python unittest准备测试数据

时间:2018-07-04 01:59:26

标签: python-3.x automated-tests python-unittest fixtures

我所有的测试都有一个父类,它是从unittest.TestCase继承的。它包含许多全局变量,以及用于创建和删除用户及其他实体的一堆自定义函数。

class AutoTest(unittest.TestCase):
    def vars(self):
       self.api = "http://"
       self.auth = HTTPBasicauth("arr", "yarrr")

    def new_user(self):
        requests.post(url, json, auth)
        return response
    """"and so on"""

问题是我需要为测试套件准备测试数据,并且应该为套件中的所有测试(即测试类)准备一次。 据我了解,在这种情况下使用setUpClass,但仅将其用作类方法,因此所有在父类ustom函数中定义的函数对于调用测试数据绝对至关重要,因为所有这些都无法调用它们是实例方法,并且具有“自我”位置参数。

class TestSomeStuff(AutoTests):

    def setUpClass(cls):
        *and here is the problem, because none of the AutoTest class functions are available*

将感谢您的帮助/建议。

1 个答案:

答案 0 :(得分:1)

使用setUpClass调用父类的super()方法。另外,您应该始终用setUpClass装饰@classmethod

class TestSomeStuff(AutoTests):
    @classmethod
    def setUpClass(cls):
        super(TestSomeStuff, cls).setUpClass()