Skip to content

Additional assert methods for unittest #71339

Closed
@serhiy-storchaka

Description

@serhiy-storchaka
BPO 27152
Nosy @rhettinger, @rbtcollins, @ezio-melotti, @bitdancer, @voidspace, @serhiy-storchaka, @PythonCHB
Files
  • extra_assertions.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 = None
    created_at = <Date 2016-05-29.15:25:19.795>
    labels = ['type-feature', 'tests']
    title = 'Additional assert methods for unittest'
    updated_at = <Date 2016-06-22.14:03:21.198>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2016-06-22.14:03:21.198>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Tests']
    creation = <Date 2016-05-29.15:25:19.795>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['43047']
    hgrepos = []
    issue_num = 27152
    keywords = ['patch']
    message_count = 10.0
    messages = ['266600', '266601', '266620', '266627', '266675', '266682', '267175', '267185', '268877', '269074']
    nosy_count = 8.0
    nosy_names = ['rhettinger', 'rbcollins', 'ezio.melotti', 'r.david.murray', 'michael.foord', 'Pam.McANulty', 'serhiy.storchaka', 'ChrisBarker']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue27152'
    versions = ['Python 3.6']

    Linked PRs

    Activity

    serhiy-storchaka

    serhiy-storchaka commented on May 29, 2016

    @serhiy-storchaka
    MemberAuthor

    Proposed patch adds the ExtraAssertions mix-in that provides additional assert methods. These methods provide better failure report than assertTrue/assertFalse with a result of corresponding function. For example assertStartsWith outputs shorten reprs of the start of the string and prefix.

    These checks are quite popular and can be used tens or hundreds times in tests (the patch makes tests using new assert methods):

    assertHasAttr 121 times
    assertNotHasAttr 99 times
    assertIsSubclass 243 times
    assertNotIsSubclass 95 times
    assertStartsWith 131 times
    assertEndsWith 73 times

    serhiy-storchaka

    serhiy-storchaka commented on May 29, 2016

    @serhiy-storchaka
    MemberAuthor

    Additional statistics that proves that specified checks are not specific for narrow group of tests:

    assertHasAttr 121 times in 57 files
    assertNotHasAttr 99 times in 31 files
    assertIsSubclass 243 times in 30 files
    assertNotIsSubclass 95 times in 5 files
    assertStartsWith 131 times in 59 files
    assertEndsWith 73 times in 36 files

    bitdancer

    bitdancer commented on May 29, 2016

    @bitdancer
    Member

    Why a mixin?

    (As an aside, there is a proposal to move all assert methods to a place where they can be accessed outside test cases.)

    serhiy-storchaka

    serhiy-storchaka commented on May 29, 2016

    @serhiy-storchaka
    MemberAuthor

    I have this proposal (bpo-19645) in mind. We can add these method just in TestCase.

    In any case I'm going to add the ExtraAssertions mixin in test.support in maintained versions to help keeping branches in sync.

    rhettinger

    rhettinger commented on May 30, 2016

    @rhettinger
    Contributor

    I don't really like the assertEndsWith method which triggers a mental hiccup when taking a familiar method call, making it into function call, and giving an awkward looking camelcase name.

    Also, Guido is usually opposed to broad, sweeping search/replace patches. Instead, he has advocated "holistic refactoring" where updates are done by someone actively working on the module rather than a disinterested party churning the code without thinking deeply about the topic at hand.

    serhiy-storchaka

    serhiy-storchaka commented on May 30, 2016

    @serhiy-storchaka
    MemberAuthor

    Changes in tests are provided as a demonstration that these methods are useful. I propose to commit only the unittest part, and use new methods on a case-by-case basis. For example some tests already define methods like assertHasAttr or assertIsSubclass, They can now use standard methods.

    assertEndsWith() can be used 73 times in 36 files. It produces more useful error report than assertTrue(a.endswith(b)).

    PythonCHB

    PythonCHB commented on Jun 3, 2016

    @PythonCHB
    Mannequin

    Why a mixin rather than adding to TestCase? If they are useful they should be easy to find.

    Also, see bpo-27198 for another possible new assert.

    rbtcollins

    rbtcollins commented on Jun 3, 2016

    @rbtcollins
    Member

    I'm fine with these as a mixin - they are all very generic and unambiguously named. I'd marginally prefer the opt-in mixin over adding them to the base class.

    Ideally they'd be matchers, but since I haven't ported that upstream yet, thats asking for more work, and we can always migrate later.

    (https://rbtcollins.wordpress.com/2010/05/10/maintainable-pyunit-test-suites/ and http://testtools.readthedocs.io/en/latest/for-test-authors.html#matchers - sorry about the formatting in the blog post, wordpress changed theme details some time after I wrote the post and it now renders horribly :( )

    rhettinger

    rhettinger commented on Jun 20, 2016

    @rhettinger
    Contributor

    Ask five people to spell "assertEndsWith" and see how many of them capitalize the "W".

    bitdancer

    bitdancer commented on Jun 22, 2016

    @bitdancer
    Member

    I would expect it to be assertEndswith, etc. You will note that all the other cases of multiple capitals are either englishification of symbolic operators or concatenations of separate type words and/or syntactically distinct operators.

    transferred this issue fromon Apr 10, 2022
    gpshead

    gpshead commented on Jun 13, 2023

    @gpshead
    Member
    No description provided.

    132 remaining items

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibPython modules in the Lib dirtestsTests in the Lib/test dirtype-featureA feature request or enhancement

      Projects

      Status

      Done

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        Additional assert methods for unittest · Issue #71339 · python/cpython

        Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

        Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant