In contrast, `assertContains` (the next method on the page) is more
explicit:
> Asserts that a Response instance produced the given status_code [...]
(though it would have been nice if `Response` was a link to the relevant
doc)
[1]
https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertFormError
[2]
https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertFormsetError
--
Ticket URL: <https://code.djangoproject.com/ticket/33301>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
OK, yes, thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/33301#comment:1>
* owner: nobody => Baptiste Mispelon
* status: new => assigned
* easy: 1 => 0
Comment:
I might have been too quick to mark this as an "easy picking"...
After digging a little, I found that the situation is a bit messier than
I'd seen at first. Several custom `assert...` methods take a response
object as a required argument, but some of them will accept any
`HttpReponse` (and subclasses) whereas some require a response object from
the test client:
||= assert =||= Type of response =||= attribute =||
||
[[https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertFormError|assertFormError]]
|| Test client || `response.context` ||
||
[[https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertFormsetError|assertFormsetError]]
|| Test client || `response.context` ||
||
[[https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertContains|assertContains]]
|| Any || ||
||
[[https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertNotContains|assertNotContains]]
|| Any || ||
||
[[https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertTemplateUsed|assertTemplateUsed]]
|| Test client || `response.templates` ||
||
[[https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertTemplateNotUsed|assertTemplateNotUsed]]
|| Test client || `response.templates` ||
||
[[https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertRedirects|assertRedirects]]
|| Any || ||
On top of that, while `assertTemplateUsed` (and `assertTemplateNotUsed`)
does specific error checking to alert the user if they're not using a test
client response, `assertFormError` (and `assertFormsetError`) do not and
simply trigger an `AttributeError` if you're passing them the wrong kind
of response.
I'll get started on a proposed fix for all these issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/33301#comment:2>
* has_patch: 0 => 1
Comment:
Here's the PR, split into 3 commits:
1) The documentation changes described in the original ticket
2) Missing tests for `assertFormError` and `assertFormsetError` (the
failure cases were not tested at all)
3) A small API change to make `assertFormError` and `assertFormsetError`
raise a `ValueError` with a helpful message (instead of an
`AttributeError`) when used with a non test-client response (same behavior
as `assertTemplateUsed`).
https://github.com/django/django/pull/15106
--
Ticket URL: <https://code.djangoproject.com/ticket/33301#comment:3>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33301#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"528691d1b66b4ecf7bbb55f783fc919d40d4a235" 528691d]:
{{{
#!CommitTicketReference repository=""
revision="528691d1b66b4ecf7bbb55f783fc919d40d4a235"
Fixed #33301 -- Clarified the type of arguments required by custom
assertions.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33301#comment:5>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"9ac92b1efc5ff90e7cce5c47fbd05887d403e4cd" 9ac92b1]:
{{{
#!CommitTicketReference repository=""
revision="9ac92b1efc5ff90e7cce5c47fbd05887d403e4cd"
Refs #33301 -- Made SimpleTestCase.assertFormError()/assertFormsetErrors()
raise ValueError for non test client responses.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33301#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"aa0c8ff9a0a0418dd3956a0be3de46e4dec956df" aa0c8ff9]:
{{{
#!CommitTicketReference repository=""
revision="aa0c8ff9a0a0418dd3956a0be3de46e4dec956df"
[4.0.x] Fixed #33301 -- Clarified the type of arguments required by custom
assertions.
Backport of 528691d1b66b4ecf7bbb55f783fc919d40d4a235 from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33301#comment:7>