Django: Ticket #5333: add an assertContext method to Django's TestCasehttps://code.djangoproject.com/ticket/5333
<p>
A method for testing that a context used for rendering a response contains a specified value. To be used as:
</p>
<div class="code"><pre><span class="bp">self</span><span class="o">.</span>assertContext<span class="p">(</span>response<span class="p">,</span> variable_name<span class="p">,</span> value<span class="p">)</span>
</pre></div>en-usDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/ticket/5333
Trac 1.0.7gwilsonTue, 04 Sep 2007 05:02:43 GMTattachment sethttps://code.djangoproject.com/ticket/5333
https://code.djangoproject.com/ticket/5333
<ul>
<li><strong>attachment</strong>
set to <em>5333.diff</em>
</li>
</ul>
TicketgwilsonTue, 04 Sep 2007 05:06:40 GMTstage changed; needs_docs, needs_better_patch, needs_tests sethttps://code.djangoproject.com/ticket/5333#comment:1
https://code.djangoproject.com/ticket/5333#comment:1
<ul>
<li><strong>needs_docs</strong>
set
</li>
<li><strong>needs_better_patch</strong>
unset
</li>
<li><strong>needs_tests</strong>
unset
</li>
<li><strong>stage</strong>
changed from <em>Unreviewed</em> to <em>Design decision needed</em>
</li>
</ul>
<p>
hmmm, not sure why the diff doesn't show in the Trac viewer.
</p>
TicketJohan BergströmTue, 04 Sep 2007 09:16:34 GMThttps://code.djangoproject.com/ticket/5333#comment:2
https://code.djangoproject.com/ticket/5333#comment:2
<p>
You probably want to name the file "5333.patch" (i had similar issues earlier)
</p>
TicketrussellmTue, 04 Sep 2007 13:39:37 GMThttps://code.djangoproject.com/ticket/5333#comment:3
https://code.djangoproject.com/ticket/5333#comment:3
<p>
My hesitation here (and the reason I didn't include a 'context assert' in the first place) is that assertContext does an Equals test, but doesn't provide a way to do any other assertion - NotEquals, True, False, LessThan, etc. We could get around this by adding assertContextEquals, assertContextNotEquals, and so on, but that is really just duplicating the underlying assertion API. This is certainly possible, but I'm not convinced it is the best approach.
</p>
<p>
An alternate approach would be to provide a utility function to get the value of a context variable from the list of contexts in a response. This would allow the use of all the standard assert methods. However, I'm not entirely sure where such a utility function should go. Putting it on the TestCase itself rubs me the wrong way.
</p>
<p>
I'm open to suggestions (or to comments that I'm just round-the-twist crazy).
</p>
<p>
For future reference, some feedback on the patch itself:
</p>
<ol><li>The test cases should check the multiple-context case, not just the single-context case
</li><li>If possible, it would also be good to provide the 'trailing space' parts of the patch separately to the new assertion+tests. As is, there is a lot of noise in the patch from all the whitespace stripping changes, which is an impediment to the immediate understanding of the patch.
</li></ol>
TicketPhiRSat, 15 Sep 2007 20:09:54 GMTkeywords sethttps://code.djangoproject.com/ticket/5333#comment:4
https://code.djangoproject.com/ticket/5333#comment:4
<ul>
<li><strong>keywords</strong>
<em>feature</em> added
</li>
</ul>
TicketmtredinnickSun, 06 Jul 2008 02:59:56 GMThttps://code.djangoproject.com/ticket/5333#comment:5
https://code.djangoproject.com/ticket/5333#comment:5
<p>
I prefer the approach Russell's talking about in comment 3. A nice way to check the value that would be used if I rendered "foo" from the context that understands context are sometimes like an onion (does that mean they're also like an ogre?).
</p>
<p>
I don't think such a function should live on TestCase, since that would hurt doctests. It has no class dependencies, so just make it a utility function. A standalone function that accepts a context and a variable name would seem to do the trick.
</p>
TicketkkubasikMon, 20 Apr 2009 21:18:19 GMTowner changedhttps://code.djangoproject.com/ticket/5333#comment:6
https://code.djangoproject.com/ticket/5333#comment:6
<ul>
<li><strong>owner</strong>
changed from <em>nobody</em> to <em>kkubasik</em>
</li>
</ul>
TicketjulienTue, 21 Apr 2009 03:43:47 GMThttps://code.djangoproject.com/ticket/5333#comment:7
https://code.djangoproject.com/ticket/5333#comment:7
<p>
Since <a class="changeset" href="https://code.djangoproject.com/changeset/10084/">[10084]</a> it is possible to easily access/control any variable in the response's context, so I presume this <tt>assertContext</tt> method is not really needed anymore. Shall this ticket be closed?
</p>
TicketrussellmTue, 21 Apr 2009 04:12:10 GMTstatus changed; resolution sethttps://code.djangoproject.com/ticket/5333#comment:8
https://code.djangoproject.com/ticket/5333#comment:8
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
</ul>
<p>
Agreed. This one is essentially resolved.
</p>
Ticket