diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-02-20 03:16:10 -0800 |
|---|---|---|
| committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2019-02-20 12:16:10 +0100 |
| commit | 7feddd878cd0d4ad6cc98f0da2b597d603a211a7 (patch) | |
| tree | c46e8a85e07348989f3afd0103123ab09dc848fa /docs | |
| parent | 7071f8f2729295b0da77b6c651966dc32c71f1ab (diff) | |
Fixed #18707 -- Added support for the test client to return 500 responses.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/releases/3.0.txt | 8 | ||||
| -rw-r--r-- | docs/topics/testing/tools.txt | 37 |
2 files changed, 41 insertions, 4 deletions
diff --git a/docs/releases/3.0.txt b/docs/releases/3.0.txt index f036969c92..77bace661b 100644 --- a/docs/releases/3.0.txt +++ b/docs/releases/3.0.txt @@ -187,7 +187,13 @@ Templates Tests ~~~~~ -* ... +* The new test :class:`~django.test.Client` argument + ``raise_request_exception`` allows controlling whether or not exceptions + raised during the request should also be raised in the test. The value + defaults to ``True`` for backwards compatibility. If it is ``False`` and an + exception occurs, the test client will return a 500 response with the + attribute :attr:`~django.test.Response.exc_info`, a tuple providing + information of the exception that occurred. URLs ~~~~ diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt index 4ffe84dd04..ea01bd026b 100644 --- a/docs/topics/testing/tools.txt +++ b/docs/topics/testing/tools.txt @@ -128,6 +128,14 @@ Use the ``django.test.Client`` class to make requests. The ``json_encoder`` argument allows setting a custom JSON encoder for the JSON serialization that's described in :meth:`post`. + The ``raise_request_exception`` argument allows controlling whether or not + exceptions raised during the request should also be raised in the test. + Defaults to ``True``. + + .. versionadded:: 3.0 + + The ``raise_request_exception`` argument was added. + Once you have a ``Client`` instance, you can call any of the following methods: @@ -476,6 +484,23 @@ Specifically, a ``Response`` object has the following attributes: :attr:`~django.template.response.SimpleTemplateResponse.context_data` may be a suitable alternative on responses with that attribute. + .. attribute:: exc_info + + .. versionadded:: 3.0 + + A tuple of three values that provides information about the unhandled + exception, if any, that occurred during the view. + + The values are (type, value, traceback), the same as returned by + Python's :func:`sys.exc_info`. Their meanings are: + + - *type*: The type of the exception. + - *value*: The exception instance. + - *traceback*: A traceback object which encapsulates the call stack at + the point where the exception originally occurred. + + If no exception occurred, then ``exc_info`` will be ``None``. + .. method:: json(**kwargs) The body of the response, parsed as JSON. Extra keyword arguments are @@ -544,9 +569,10 @@ content type of a response using ``response['Content-Type']``. Exceptions ---------- -If you point the test client at a view that raises an exception, that exception -will be visible in the test case. You can then use a standard ``try ... except`` -block or :meth:`~unittest.TestCase.assertRaises` to test for exceptions. +If you point the test client at a view that raises an exception and +``Client.raise_request_exception`` is ``True``, that exception will be visible +in the test case. You can then use a standard ``try ... except`` block or +:meth:`~unittest.TestCase.assertRaises` to test for exceptions. The only exceptions that are not visible to the test client are :class:`~django.http.Http404`, @@ -555,6 +581,11 @@ The only exceptions that are not visible to the test client are exceptions internally and converts them into the appropriate HTTP response codes. In these cases, you can check ``response.status_code`` in your test. +If ``Client.raise_request_exception`` is ``False``, the test client will return a +500 response as would be returned to a browser. The response has the attribute +:attr:`~Response.exc_info` to provide information about the unhandled +exception. + Persistent state ---------------- |
