但是,哪种方法是创建测试对象的首选方法?为什么?你知道吗
说明:出于测试目的,即测试在test_models.py
文件中创建的model
第一条路:
使用@classmethods
class AuthorModelTest(TestCase):
@classmethod
def setUpTestData(cls):
Author.objects.create(first_name="Big", last_name="Bob")
第二条路:
或者传递self
而不是引用class
class AuthorModelTest(TestCase):
def setUpTestData(self):
Author.objects.create(first_name="Big", last_name="Bob")
它在
TestCase
中被定义为classmethod,因此您应该在代码中执行同样的操作。也许这两个版本现在都可以工作,但是在Django的未来版本中,它会破坏您的代码与Django的兼容性。您可以检查documentation。你知道吗只需遵循文档中的示例:
相关问题 更多 >
编程相关推荐