【问题标题】:Testing method that returns relation model attribute返回关系模型属性的测试方法
【发布时间】:2020-06-16 11:01:04
【问题描述】:

我有以下型号:

class Course(Base):

    title = models.CharField('Título', max_length=200)
    subcategory = models.ForeignKey(SubCategory, on_delete=models.CASCADE)

    def get_category(self):
        return self.subcategory.category


class SubCategory(Base):
    title = models.CharField('Título', max_length=80)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)


class Category(Base):
    title = models.CharField('Título', max_length=80)

如何测试 get_category 方法?

我正在尝试这样做,但测试未通过覆盖率报告。

【问题讨论】:

  • 请分享您尝试了什么?

标签: python django unit-testing testing


【解决方案1】:

类似的东西。

class CourseModelTest(TestCase):
    def test_get_category(self):
        category = CategoryFactory()
        subcategory = SubCategoryFactory(category=category)
        course = CourseFactory(subcategory=subcategory)

        self.assertEqual(course.get_category().id, category.id)

【讨论】:

  • 我的错。我没有调用该函数进行测试。谢谢塔拉斯。
猜你喜欢
  • 2016-07-21
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多