#17604 closed New feature (fixed)
Make use of assertTemplateUsed and assertTemplateNotUsed as ContextManagers
| Reported by: | Gregor Müllegger | Owned by: | Gregor Müllegger |
|---|---|---|---|
| Component: | Testing framework | Version: | 1.3 |
| Severity: | Normal | Keywords: | assertTemplateUsed, assertTemplateNotUsed, contextmanager |
| Cc: | kmike84@… | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
It can be useful to use the assertTemplateUsed assertion on a limited block of code via a context manager instead on a request:
with self.assertTemplateUsed('template_used/base.html'):
render_to_string('template_used/base.html')
This is something that already proofed useful in the soc2011/form-rendering branch. It's implemented there and is well tested: https://github.com/gregmuellegger/django/blob/soc2011/form-rendering/django/test/testcases.py#L720
Same is true for assertTemplateNotUsed.
Attachments (2)
Change History (10)
comment:1 by , 14 years ago
by , 14 years ago
| Attachment: | 17604-assertTemplateUsed.1.diff added |
|---|
comment:2 by , 14 years ago
| Has patch: | set |
|---|
comment:3 by , 14 years ago
| Cc: | added |
|---|
comment:4 by , 14 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:5 by , 14 years ago
| Patch needs improvement: | set |
|---|
I'm seeing three errors and one failure in the tests with the latest patch: http://paste.pocoo.org/show/543635/
comment:6 by , 14 years ago
| Patch needs improvement: | unset |
|---|
Did you applied the patch with "patch -p1 < t17604-assertTemplateUsed.2.diff"? I tried so and had the same problem, since it doesn't create the two empty files:
tests/regressiontests/test_utils/templates/template_used/base.html tests/regressiontests/test_utils/templates/template_used/alternative.html
Use git apply t17604-assertTemplateUsed.2.diff instead or create those empty files by hand.
Sorry, I don't know how to make the patch work otherwise :-(
comment:8 by , 14 years ago
Oops, my bad :-) Thanks for the pointer on getting the patch applied properly.
The feature is implemented here: https://github.com/gregmuellegger/django/tree/t17604-assertTemplateUsed
The compare view: https://github.com/gregmuellegger/django/compare/master...t17604-assertTemplateUsed
Would be good if anyone could in particular review the docs, since my english is not the best at the moment.