我正在使用 TestCase 为我的 django 应用程序编写测试,并且希望能够将参数传递给父类的 setUp 方法,如下所示:
from django.test import TestCase
class ParentTestCase(TestCase):
def setUp(self, my_param):
super(ParentTestCase, self).setUp()
self.my_param = my_param
def test_something(self):
print('hello world!')
class ChildTestCase(ParentTestCase):
def setUp(self):
super(ChildTestCase, self).setUp(my_param='foobar')
def test_something(self):
super(ChildTestCase, self).test_something()
但是,我收到以下错误:
TypeError: setUp() takes exactly 2 arguments (1 given)
我知道这是因为只有 self 仍然通过,并且我需要覆盖类__init__
才能使其正常工作。我是 Python 的新手,不知道如何实现这一点。任何帮助表示赞赏!