This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Patch all tests to change assertTrue(a [not] in b [, c]) -> assert[Not]In(a, b [,c])
Type: enhancement Stage: resolved
Components: Tests Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, brian.curtin, dmalcolm, ezio.melotti
Priority: low Keywords: patch

Created on 2010-01-18 23:24 by dmalcolm, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
py3k-use-assertIn-and-NotIn-in-tests.patch dmalcolm, 2010-01-18 23:24
Messages (3)
msg98039 - (view) Author: Dave Malcolm (dmalcolm) (Python committer) Date: 2010-01-18 23:24
In many places throughout the existing test suite, there are assertions of the form:
     self.assertTrue(chips in menu)
and of the form:
     self.assertTrue(cheese not in shop)

If these fail, the error message will merely tell you that the condition failed, without telling you the values.

Some of these tests have an additional handcoded "msg" argument.

The attached patch (to the py3k branch) changes all of these assertions to use the assertIn and assertNotIn methods: when these fail, the log will indicate the repr() of the left- and right- hand sides.

If a "msg" argument was provided which isn't the left or right-hand side, I supply that as a third argument (which these methods can also accept).

I generated this patch using this pair of shell commands:

# Fixup "not in":
find Lib/test -name test_\*.py \
    | xargs sed --in-place -r -e "s|self.assertTrue\((.*) not in (.*)\)|self.assertNotIn(\1, \2)|g"

# Fixup "in":
find Lib/test -name test_\*.py \
    | xargs sed --in-place -r -e "s|self.assertTrue\((.*) in (.*)\)|self.assertIn(\1, \2)|g"

and then manually fixing the tests until they worked (the above isn't perfect, but was close enough, if I had to do it again I'd write a python script).

I edited some of the basic tests for collections that verify in/not-in semantics, so that both forms of test are present (using the assertIn variant first, to provide better logs in the event of failure.  I'm not sure that doing so is meaningful though (assertIn does a "member not in container", and assertNotIn does a "member in container" test).

I've rerun the test on my local build, and verified the code visually (however the patch is rather large so I may have missed something):
  75 files changed, 471 insertions(+), 454 deletions(-)

(suggested by http://bugs.python.org/msg97856 )
msg98042 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-01-19 00:13
Thanks for the patch! Applied in r77604.
msg98202 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-01-24 01:16
I fixed more in r77111 and then backported everything to trunk in r77715.
History
Date User Action Args
2022-04-11 14:56:56adminsetgithub: 51986
2010-01-24 01:16:17ezio.melottisetnosy: + ezio.melotti
messages: + msg98202

keywords: - needs review
stage: patch review -> resolved
2010-01-19 00:13:13benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg98042

resolution: accepted
2010-01-18 23:51:37brian.curtinsetnosy: + brian.curtin
versions: + Python 2.7
priority: low
keywords: + needs review
type: enhancement
stage: patch review
2010-01-18 23:24:47dmalcolmcreate