【问题标题】:Testing a Django Generic View测试 Django 通用视图
【发布时间】:2016-05-10 15:50:01
【问题描述】:

我刚开始使用 Django,我真的很想养成测试所有我应该做的事情的习惯。我现在的大多数类都是基于类的通用视图,例如列表视图。这是一个这样的:

class CellView(ListView):
    """
    Class that determines list view for all Cells

    Inherits from Django GenericView ListView Class

    Attributes:
        model: Overrided attribute that indicates that
             cell is the model to be displayed
        template_name: Overrided attribute that indicates
            which html template to use  
        queryset: Overrided attribute that indicates a python
            queryset of all cell objects to be displated
        context_object_name: Overrided attribute that indicates
            string to name the context object in the template
   """

   model = Cell
   template_name = "CellView.html"
   query_set = Cell.objects.all()
   context_object_name = "Cells"

我没有任何自定义方法,我在这里所做的只是定义属性。我应该在这里测试这段代码吗?或者这一切真的算作 Django 处理的事情,我应该相信它会起作用吗?如果我应该测试它,我应该具体测试什么?

【问题讨论】:

    标签: python django django-class-based-views django-testing


    【解决方案1】:

    我想说这仍然值得测试,但可能是在功能测试级别。也就是说,在您的测试中创建一些数据,然后使用测试客户端请求相关页面并断言它包含您期望的文本。

    【讨论】:

    • 同意。这里的单元测试之类的东西都是测试实现(甚至是配置?),而不是实际的逻辑。 FWIW,query_set 参数是多余的。 self.model.objects.all() 是默认 query_set
    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2019-01-05
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多