简体   繁体   中英

Testing a Django Generic View

I'm just starting out with Django, and I really want to get in the habbit of testing all that I should. Most of my classes right now are class based generic views, such as list view for example. Here is one such:

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"

I don't have any custom methods, and all I do here is define attributes. Should I be testing this code here? Or does this all really count as something Django handles and I should trust that it will work? If I should be testing it, what specifically should I be testing?

I'd say this is still worth testing, but probably at the level of a functional test. That is, in your test create some data then use the test client to request the relevant page and assert that it contains the text you were expecting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM