Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch all tests to change assertTrue(a [not] in b [, c]) -> assert[Not]In(a, b [,c]) #51986

Closed
davidmalcolm opened this issue Jan 18, 2010 · 3 comments
Labels
tests Tests in the Lib/test dir type-feature A feature request or enhancement

Comments

@davidmalcolm
Copy link
Member

BPO 7737
Nosy @benjaminp, @ezio-melotti, @briancurtin, @davidmalcolm
Files
  • py3k-use-assertIn-and-NotIn-in-tests.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-01-19.00:13:13.363>
    created_at = <Date 2010-01-18.23:24:47.071>
    labels = ['type-feature', 'tests']
    title = 'Patch all tests to change assertTrue(a [not] in b [, c]) -> assert[Not]In(a, b [,c])'
    updated_at = <Date 2010-01-24.01:16:17.030>
    user = 'https://github.com/davidmalcolm'

    bugs.python.org fields:

    activity = <Date 2010-01-24.01:16:17.030>
    actor = 'ezio.melotti'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-01-19.00:13:13.363>
    closer = 'benjamin.peterson'
    components = ['Tests']
    creation = <Date 2010-01-18.23:24:47.071>
    creator = 'dmalcolm'
    dependencies = []
    files = ['15942']
    hgrepos = []
    issue_num = 7737
    keywords = ['patch']
    message_count = 3.0
    messages = ['98039', '98042', '98202']
    nosy_count = 4.0
    nosy_names = ['benjamin.peterson', 'ezio.melotti', 'brian.curtin', 'dmalcolm']
    pr_nums = []
    priority = 'low'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue7737'
    versions = ['Python 2.7', 'Python 3.2']

    @davidmalcolm
    Copy link
    Member Author

    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 )

    @davidmalcolm davidmalcolm added the tests Tests in the Lib/test dir label Jan 18, 2010
    @briancurtin briancurtin added the type-feature A feature request or enhancement label Jan 18, 2010
    @benjaminp
    Copy link
    Contributor

    Thanks for the patch! Applied in r77604.

    @ezio-melotti
    Copy link
    Member

    I fixed more in r77111 and then backported everything to trunk in r77715.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    tests Tests in the Lib/test dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants