我正在创建 Django 单元测试,并且有一些非常相似的模型具有重复的功能。但是有一些不同,所以我创建了一个抽象的 BaseTestCase 类,其他 TestCase 类从中继承。它似乎工作正常,除了当从 BaseTestCase 类继承的 TestCases 完成运行它们的测试时,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 个测试,5 个测试用于 5 个测试用例,但它运行了 30 个。在为每个子类运行 5 个测试之后,它也为基础测试用例运行 5 个测试。
最佳答案
from django.test import TestCase
class BaseTestCase:
# ...
class ChildTestCaseN(BaseTestCase, TestCase):
# ...
我在 Django 中找不到任何关于抽象测试用例的信息。您从哪里得到 SetupTestCase
类?
关于django - 也停止运行测试的抽象 django.test TestCase 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47401578/