【问题标题】:How to test model method django如何测试模型方法django
【发布时间】:2016-04-12 10:06:41
【问题描述】:

我给我的应用写了测试,但我不知道如何测试这个模型方法:

def has_recipe(self):
    if self.type in [101, 102, 103, 104, 201, 301, 302, 303, 304]:
        return True
    else:
        return False

当我这样写测试时:

def test_has_recipe(self):
    book = Book(type in [101, 102, 103, 104, 201, 301, 302, 303, 304])
    self.assertTrue(book, [101, 102, 103, 104, 201, 301, 302, 303, 304])

测试通过,但没有覆盖。

【问题讨论】:

    标签: django testing django-models


    【解决方案1】:

    您需要在测试中创建一本书,然后调用其has_recipe 方法。

    你可以试试这样的:

    def test_has_recipe(self):
        book = Book(type=101)
        self.assertTrue(book.has_recipe())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多