Opened 22 months ago

Last modified 8 months ago

#20456 assigned New feature

Easier unit testing for class-based views

Reported by: benoitbryon Owned by: mjtamlyn
Component: Testing framework Version: master
Severity: Normal Keywords: cbv test
Cc: bmispelon@…, marc.tamlyn@…, Natim, unai@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


When django users create class-based views, they create custom code. Testing this code matters. Writing unit tests for the custom code (and only for the custom code) is important.

As of Django 1.5, the easiest way to test class-based views is using the builtin test client. But it performs integration tests, i.e. involves middlewares, URL resolvers, decorators...
It is also quite easy to use django.test.RequestFactory and as_view() classmethod. But, since as_view() returns a function, the tests can only check the response. It means the class-based views are tested as a system.

It seems that writing unit tests is possible, i.e. we can write unit tests for class-based views methods.
There is at least one technique which was presented at DjangoCon Europe 2013 Warsaw:

The recipe mentioned above seems to work (to be confirmed). But wouldn't be better if the recipe was documented in Django's documentation ( I guess) and tools (setup_view) were builtin Django?

Note: perhaps, it would be even better if setup_view() is not a standalone function but is a method of View class. Something like as_view(), but which returns an instance.

Change History (9)

comment:1 Changed 22 months ago by bmispelon

  • Cc bmispelon@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 22 months ago by mjtamlyn

  • Cc marc.tamlyn@… added
  • Keywords cbv test added
  • Owner changed from nobody to mjtamlyn
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted

Accepted in principle, not necessarily the design.

comment:3 Changed 21 months ago by Natim

  • Cc Natim added

comment:4 Changed 15 months ago by benoitbryon

I have 2 use cases for this feature:

  • As a library developer, I want to test generic class-based views without having to register them in URLconf.
  • As a project developer, given I override some generic class-based view in order to customize something (such as "get_template_names" or "get_context_data" methods), I want to test only the methods I overrid (I suppose other methods are covered elsewhere).

Here is an example TestCase in django-downloadview:
The idea is that PathDownloadView customizes get_file() method, so, I want to test that. Notice that django-downloadview is a library that does not provide URLconf, so I appreciate to test views without having to setup some fake URLconf.

comment:5 Changed 15 months ago by benoitbryon

Perhaps this feature would make it possible to simplify tests around Django's generic views:

comment:6 Changed 14 months ago by unaizalakain

  • Cc unai@… added

comment:7 Changed 14 months ago by anonymous

How about something like MyCBView.as_object(request, *args, **kwargs) instead of the setup_view approach?

comment:8 Changed 12 months ago by bmispelon

  • Has patch set

Someone made a proposal here:

comment:9 Changed 8 months ago by timo

  • Patch needs improvement set
Note: See TracTickets for help on using tickets.
Back to Top