【问题标题】:Stop abstract django.test TestCase class from running test also停止运行测试的抽象 django.test TestCase 类
【发布时间】:2017-11-20 21:41:16
【问题描述】:

我正在创建 Django 单元测试,并且有一些具有重复功能的非常相似的模型。但是有一些差异,所以我创建了一个抽象 BaseTestCase 类,其他 TestCase 类继承自该类。它似乎工作正常,除了当从 BaseTestCase 类继承的 TestCase 完成运行他们的测试时,Django 仍然会尝试运行 BaseTestCase 类测试。 Django 不应该不想在抽象 BaseTestCase 上运行测试吗?我是否缺少某种类型的配置来确保不会发生这种情况?

测试用例布局

class BaseTestCase(SetupTestCase):
    api = ""

    def test_obj_list(self):
        response = self.client.get(self.api)

        self.assertTrue(response.status_code == 200)

    def test_obj_create(self):
        pass

    def test_obj_retrieve(self):
        pass

    def test_obj_update(self):
        pass

    def test_obj_delete(self):
        pass

    class Meta:
        abstract = True

class ChildTestCaseOne(BaseTestCase):
    api = "/api/v0.1/path_one/"

class ChildTestCaseTwo(BaseTestCase):
    api = "/api/v0.1/path_two/"

class ChildTestCaseThree(BaseTestCase):
    api = "/api/v0.1/path_three/"

class ChildTestCaseFour(BaseTestCase):
    api = "/api/v0.1/path_four/"

class ChildTestCaseFive(BaseTestCase):
    api = "/api/v0.1/path_five/"

这应该运行 25 个测试,五个测试用例的五个测试,但它运行 30 个。在为每个子类运行 5 个测试之后,它也为基本测试用例运行 5 个测试。

【问题讨论】:

    标签: django unit-testing abstract-class testcase


    【解决方案1】:

    multiple inheritance 呢?

    from django.test import TestCase
    
    class BaseTestCase:
        # ...
    
    class ChildTestCaseN(BaseTestCase, TestCase):
        # ...
    

    我在 Django 中找不到任何关于抽象测试用例的信息。你从哪里得到 SetupTestCase 类的?

    【讨论】:

    • 成功了,谢谢 luvejo !它继续运行测试是有道理的,因为它是从 Django 的 TestCase 类继承的,有些日子我只是得到了隧道视野:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多