[New-bugs-announce] [issue7737] Patch all tests to change assertTrue(a [not] in b [, c]) -> assert[Not]In(a, b [, c])

Dave Malcolm report at bugs.python.org
Tue Jan 19 00:24:48 CET 2010

New submission from Dave Malcolm <dmalcolm at redhat.com>:

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 )

components: Tests
files: py3k-use-assertIn-and-NotIn-in-tests.patch
keywords: patch
messages: 98039
nosy: dmalcolm
severity: normal
status: open
title: Patch all tests to change assertTrue(a [not] in b [, c]) -> assert[Not]In(a, b [,c])
versions: Python 3.2
Added file: http://bugs.python.org/file15942/py3k-use-assertIn-and-NotIn-in-tests.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list