【问题标题】:python unittest prepare test datapython unittest 准备测试数据
【发布时间】:2018-07-04 01:59:25
【问题描述】:

我的所有测试都有一个父类,它继承自 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 函数中,这对于测试数据准备绝对至关重要,因为它们都是不可调用的实例方法并具有“self”位置参数。

class TestSomeStuff(AutoTests):

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

将不胜感激任何帮助/建议。

【问题讨论】:

  • 使用 pytest:docs.pytest.org/en/latest MUCH MUCH 更好....
  • @StephenRauch 同意。但到目前为止必须使用单元测试工作

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


【解决方案1】:

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

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

【讨论】:

  • 是的,我知道装饰器和 super()。主要问题是只运行一次 setUp 并保持对具有“自我”位置参数的父类函数和变量的访问
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 2015-07-02
相关资源
最近更新 更多