This is the standard Django tutorial application with some extra tests.
High level: provide examples of testing Django views, especially GET &
POST requests, using Django test Client
and Django RequestFactory
.
The top-level difference between the two is that Client
creates and
runs the request starting at low-level HTTP, to middleware, to view,
and returns the response object with the context, used to render
template.
On the other hand, RequestFactory
returns request object that can be
then used to test view function as a black-box, with exactly known
inputs, testing for specific outputs.
Although both have their purpose, request factory based tests are much
faster than client, as can be observed by cloning this repository and
running tox
:
$ tox -- polls.tests.test_views_with_request_factory
... output ...
Ran 9 tests in 0.030s
$ tox -- polls.tests.test_views_with_client
... output ...
Ran 10 tests in 0.205s
- using
Client
: polls/tests/test_views_with_client.py - using
RequestFactory
: polls/tests/test_views_with_request_factory.py