class MotherTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.my_value = "foo"
class ChildTestCase(MotherTestCase):
def test_basic(self):
self.assertEqual(self.my_value, "foo")
运行此测试时,我收到 AttributeError: 'ChildTestCase' object has no attribute 'my_value'
你如何解释这一点?我想我需要调用 super()
但 Django doc没这么说
我看到了related issue在 Github 上,但它已有 1 岁了,似乎已修复。
注意:我使用的是 Python 3.5.2 和 Django 1.9.10
最佳答案
这有点令人困惑,但是有 2 个不同的 TestCase
类。其中有 unittest.TestCase
和 Django 测试用例类 django.test.TestCase
。为了使其工作,您必须从 django.test
导入,而不是 unitttest。
关于python - Django setUpTestData() 不适用于继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40399277/