Opened 6 years ago

Last modified 11 months ago

#20456 new New feature

Easier unit testing for class-based views

Reported by: Benoît Bryon Owned by:
Component: Testing framework Version: master
Severity: Normal Keywords: cbv test
Cc: bmispelon@…, marc.tamlyn@…, Rémy Hubscher, unai@…, Sergey Fedoseev 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 (12)

comment:1 Changed 6 years ago by Baptiste Mispelon

Cc: bmispelon@… added

comment:2 Changed 6 years ago by Marc Tamlyn

Cc: marc.tamlyn@… added
Keywords: cbv test added
Owner: changed from nobody to Marc Tamlyn
Status: newassigned
Triage Stage: UnreviewedAccepted

Accepted in principle, not necessarily the design.

comment:3 Changed 6 years ago by Rémy Hubscher

Cc: Rémy Hubscher added

comment:4 Changed 6 years ago by Benoît Bryon

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 6 years ago by Benoît Bryon

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

comment:6 Changed 6 years ago by Unai Zalakain

Cc: unai@… added

comment:7 Changed 6 years ago by anonymous

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

comment:8 Changed 5 years ago by Baptiste Mispelon

Has patch: set

Someone made a proposal here:

comment:9 Changed 5 years ago by Tim Graham

Patch needs improvement: set

comment:12 Changed 3 years ago by Eron Lloyd

Any update on this? I'm planning on moving to CBVs but this is concerning for my testing needs.

comment:13 Changed 3 years ago by Tim Graham

Owner: Marc Tamlyn deleted
Status: assignednew

I don't see anyone actively working on it. Feel free to work on it if you like.

comment:14 Changed 11 months ago by Sergey Fedoseev

Cc: Sergey Fedoseev added
Note: See TracTickets for help on using tickets.
Back to Top