首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止抽象django.test TestCase类运行测试

停止抽象django.test TestCase类运行测试
EN

Stack Overflow用户
提问于 2017-11-20 21:41:16
回答 1查看 965关注 0票数 2

我正在创建Django单元测试,并且有一些具有重复功能的非常相似的模型。但是,有一些不同之处,所以我创建了一个抽象的BaseTestCase类,由其他TestCase类继承而来。除了当继承自BaseTestCase类的BaseTestCase完成运行测试时,Django还将尝试运行BaseTestCase类测试之外,它似乎运行得很好。Django不应该不想在抽象的BaseTestCase上运行测试吗?我是否遗漏了某种类型的配置,以确保不会发生这种情况?

测试用例布局

代码语言:javascript
复制
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个测试,5个测试用例的5个测试,但它运行30个。在为每个子类运行了5个测试之后,它还为基本测试用例运行了5个测试。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-21 01:27:58

多重继承

代码语言:javascript
复制
from django.test import TestCase

class BaseTestCase:
    # ...

class ChildTestCaseN(BaseTestCase, TestCase):
    # ...

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47401578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档