【发布时间】: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