Django setUpTestData()不能使用继承?

2024-10-01 09:16:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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(),但是{a1}没有这么说

我在Github上看到了一个related issue,但它已经有一年的历史了,似乎已经修复了。在

注意:我使用的是python3.5.2和Django 1.9.10


Tags: testselfbasicfoovaluemydeftestcase
1条回答
网友
1楼 · 发布于 2024-10-01 09:16:04

这有点令人困惑,但是有两个不同的TestCase类。有unittest.TestCase,还有Django测试用例类django.test.TestCase。为了使其工作,必须从django.test导入,而不是从unitttest导入。在

相关问题 更多 >