#10314 closed (fixed)
TestCase assert methods do not accept a msg parameter
| Reported by: | wwinham | Owned by: | nobody |
|---|---|---|---|
| Component: | Testing framework | Version: | 1.0 |
| Severity: | Keywords: | ||
| Cc: | christian.oudard@… | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | yes |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
django.test.TestCase assert methods, such as assertContains, do not accept a failure message as their final parameter, like the underlying unittest methods do.
Attachments (5)
Change History (16)
follow-up: 2 comment:1 by , 17 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 17 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
Replying to Alex:
No reason we shouldn't keep the same API.
Django's test cases have the possibility of having more than one Exception being raised so it doesn't make sense for a single message to characterize the failure of the test.
by , 16 years ago
| Attachment: | assert_msg_patch added |
|---|
comment:3 by , 16 years ago
| Resolution: | invalid |
|---|---|
| Status: | closed → reopened |
Attached a patch which adds msg parameters to all django test case assert* functions.
I didn't understand at first what you meant by having more than one exception raised, but now that I got into the code, I do. There are multiple ways that each assert method can fail. For example, in assertContains, it could have the wrong status code, not contain the text, or not contain the text the correct number of times. Each of these three outcomes has a different default message.
However, I believe that there are cases where each of the default messages is inadequate. Take for example, a test that looks through pages for invalid template variables:
class TemplateTestCase(ClientTestCase):
def testTemplateError(self):
urls = [
'/',
'/home/',
'/admin/',
# etcetera ...
]
for url in urls:
response = self.client.get(url, follow=True)
self.assertNotContains(
response,
settings.TEMPLATE_STRING_IF_INVALID,
msg='Found an invalid variable in url %s' % url)
Without the message parameter, it would require print statements to determine which url actually failed.
by , 16 years ago
different way of doing it, without the method decorators
comment:4 by , 16 years ago
| Has patch: | set |
|---|
comment:5 by , 16 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | reopened → closed |
As sebleier noted, this is an ill-posed request. Assertions like assertFormError raised many different error messages, so a simple one-message parameterization will be insufficient.
If there is a problem with a specific error message (as is suggested by comment 3), the the solution is to fix that error message. Suggestions welcome.
comment:6 by , 16 years ago
How would you suggest handling use cases such as the above code snippet without a msg parameter?
by , 16 years ago
| Attachment: | msg_prefix_patch added |
|---|
Prefixes error messages with custom message instead of replacing it
comment:8 by , 16 years ago
| Needs documentation: | set |
|---|---|
| Resolution: | wontfix |
| Status: | closed → reopened |
Reopening following discussion on django-dev that resolved to use the extra argument as a prefix to the internally generated error message of the assertions.
comment:10 by , 16 years ago
| Resolution: | → fixed |
|---|---|
| Status: | reopened → closed |
No reason we shouldn't keep the same API.