Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#17604 closed New feature (fixed)

Make use of assertTemplateUsed and assertTemplateNotUsed as ContextManagers

Reported by: gregmuellegger Owned by: gregmuellegger
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)

17604-assertTemplateUsed.1.diff (12.9 KB) - added by gregmuellegger 4 years ago.
17604-assertTemplateUsed.2.diff (13.0 KB) - added by gregmuellegger 4 years ago.
merged with trunk

Download all attachments as: .zip

Change History (10)

comment:1 Changed 4 years ago by gregmuellegger

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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.

Changed 4 years ago by gregmuellegger

comment:2 Changed 4 years ago by gregmuellegger

  • Has patch set

comment:3 Changed 4 years ago by kmike

  • Cc kmike84@… added

comment:4 Changed 4 years ago by julien

  • Triage Stage changed from Unreviewed to Accepted

Changed 4 years ago by gregmuellegger

merged with trunk

comment:5 Changed 4 years ago by carljm

  • 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 Changed 4 years ago by gregmuellegger

  • 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:7 Changed 4 years ago by carljm

  • Resolution set to fixed
  • Status changed from new to closed

In [17412]:

Fixed #17604 - Added context-manager capability to assertTemplateUsed and assertTemplateNotUsed. Thanks Greg Müllegger.

comment:8 Changed 4 years ago by carljm

Oops, my bad :-) Thanks for the pointer on getting the patch applied properly.

Note: See TracTickets for help on using tickets.
Back to Top