Code

Opened 7 years ago

Closed 5 years ago

#5333 closed (fixed)

add an assertContext method to Django's TestCase

Reported by: gwilson Owned by: kkubasik
Component: Testing framework Version: master
Severity: Keywords: feature
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

A method for testing that a context used for rendering a response contains a specified value. To be used as:

self.assertContext(response, variable_name, value)

Attachments (1)

5333.diff (11.9 KB) - added by gwilson 7 years ago.

Download all attachments as: .zip

Change History (9)

Changed 7 years ago by gwilson

comment:1 Changed 7 years ago by gwilson

  • Needs documentation set
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

hmmm, not sure why the diff doesn't show in the Trac viewer.

comment:2 Changed 7 years ago by Johan Bergström

You probably want to name the file "5333.patch" (i had similar issues earlier)

comment:3 Changed 7 years ago by russellm

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.

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.

I'm open to suggestions (or to comments that I'm just round-the-twist crazy).

For future reference, some feedback on the patch itself:

  1. The test cases should check the multiple-context case, not just the single-context case
  2. 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.

comment:4 Changed 7 years ago by PhiR

  • Keywords feature added

comment:5 Changed 6 years ago by mtredinnick

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?).

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.

comment:6 Changed 5 years ago by kkubasik

  • Owner changed from nobody to kkubasik

comment:7 Changed 5 years ago by julien

Since [10084] it is possible to easily access/control any variable in the response's context, so I presume this assertContext method is not really needed anymore. Shall this ticket be closed?

comment:8 Changed 5 years ago by russellm

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

Agreed. This one is essentially resolved.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.