GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Additional assert methods for unittest · Issue #71339 · python/cpython

Additional assert methods for unittest #71339

New issue
Copy link
New issue
Copy link
Closed
Closed
Additional assert methods for unittest#71339
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytestsTests in the Lib/test dirTests in the Lib/test dirtype-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on May 29, 2016
Issue body actions
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

    • gh-71339: Add additional assertion methods for unittest #128707
    • [3.13] gh-71339: Add additional assertion methods in test.support (GH-128707) #128815
    • gh-71339: Improve error report for types in assertHasAttr() and assertNotHasAttr() #128818
    • gh-71339: Use assertIsSubclass() and assertNotIsSubclass() in test_collections #128824
    • gh-71339: Use new assertion methods in test_typing #128825
    • gh-71339: Use new assertion methods in test_abc #128826
    • gh-71339: Use assertIsSubclass() and assertNotIsSubclass() in test_decimal #128827
    • gh-71339: Use new assertion methods in test_logging #128828
    • gh-71339: Use new assertion methods in test_functools #128829
    • gh-71339: Use new assertion methods in test_sqlite3 #128830
    • gh-71339: Use new assertion methods in the multiprocessing tests #128847
    • gh-71339: Use new assertion methods in test_traceback #128848
    • gh-71339: Use new assertion methods in tests #129046
    • gh-71339: Use new assertion methods in test_asyncio #129051
    • gh-71339: Use new assertion methods in test_capi #129053
    • gh-71339: Use new assertion methods in test_ctypes #129054
    • gh-71339: Use new assertion methods in the email tests #129055
    • gh-71339: Use new assertion methods in the urllib tests #129056
    • gh-71339: Use new assertion methods in the http tests #129058
    • [3.12] gh-71339: Add additional assertion methods in test.support (GH-128707) (GH-128815) #129059
    • gh-71339: Use new assertion methods in test_import and test_importlib #129052
    • [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) #129123
    • gh-71339: Use new assertion methods in test_enum #129128
    • gh-71339: Fix an order-dependent failure in test_unittest #129133
    • gh-71339: Use new assertion methods in test_idle #129213
    • [3.13] gh-71339: Use new assertion methods in test_idle (GH-129213) #129237
    • [3.12] gh-71339: Use new assertion methods in test_idle (GH-129213) #129238
    • [3.13] gh-71339: Use new assertion methods in test_idle #129314
    • [3.12] gh-71339: Use new assertion methods in test_idle (GH-129314) #129315
    • [3.13] gh-71339: Use new assertion methods in the urllib tests (GH-129056) #132499
    • [3.13] gh-71339: Use new assertion methods in the http tests (GH-129058) #132500
    • [3.13] gh-71339: Use new assertion methods in the email tests (GH-129055) #132501
    • [3.14] gh-71339: Use new assertion methods in tests (GH-129046) #134498

    Activity

    Next
    1. serhiy-storchaka commented on May 29, 2016

      @serhiy-storchaka
      serhiy-storchaka
      on May 29, 2016
      MemberAuthor
      More actions

      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

    2. serhiy-storchaka
      added
      testsTests in the Lib/test dirTests in the Lib/test dir
      type-featureA feature request or enhancementA feature request or enhancement
      on May 29, 2016
    3. serhiy-storchaka commented on May 29, 2016

      @serhiy-storchaka
      serhiy-storchaka
      on May 29, 2016
      MemberAuthor
      More actions

      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

    4. bitdancer commented on May 29, 2016

      @bitdancer
      bitdancer
      on May 29, 2016
      Member
      More actions

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

    5. serhiy-storchaka commented on May 29, 2016

      @serhiy-storchaka
      serhiy-storchaka
      on May 29, 2016
      MemberAuthor
      More actions

      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.

    6. rhettinger commented on May 30, 2016

      @rhettinger
      rhettinger
      on May 30, 2016
      Contributor
      More actions

      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.

    7. serhiy-storchaka commented on May 30, 2016

      @serhiy-storchaka
      serhiy-storchaka
      on May 30, 2016
      MemberAuthor
      More actions

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

    8. PythonCHB commented on Jun 3, 2016

      @PythonCHB
      PythonCHBmannequin
      on Jun 3, 2016
      Mannequin
      More actions

      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.

    9. rbtcollins commented on Jun 3, 2016

      @rbtcollins
      rbtcollins
      on Jun 3, 2016
      Member
      More actions

      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 :( )

    10. rhettinger commented on Jun 20, 2016

      @rhettinger
      rhettinger
      on Jun 20, 2016
      Contributor
      More actions

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

    11. bitdancer commented on Jun 22, 2016

      @bitdancer
      bitdancer
      on Jun 22, 2016
      Member
      More actions

      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.

    12. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    13. serhiy-storchaka
      added this to Unittest & doctest issueson May 20, 2022
    14. gpshead commented on Jun 13, 2023

      @gpshead
      gpshead
      on Jun 13, 2023
      Member
      More actions

      absl.testing.absltest includes some of these methods.

      In particular assertStartsWith and assertEndsWith (and Not variants) are its simplest addition. I "only" see those used 2-3000 test files in our huge internal codebase which puts it in a <10% use them category. Their goal is to provide a nice error message, it does that. Those 2-4 would be trivial additions with no maintenance burden. (I'd go with 2, the negative Not variants see almost no use in practice).

      Outside of the stdlib I assume pytest may provide nice output on startswith and endwith comparison failures?

      As for the method name spelling, anyone typing it wrong gets a failure immediately so that isn't a big deal, rapid feedback when wrong. They're fully consistent with the junit javaCaseStyle of unittest assertion methods with words delineated by a case change.

    15. 87 remaining items

    16. serhiy-storchaka
      added 3 commits that reference this issue on Apr 14, 2025

      pythongh-71339: Use new assertion methods in the email tests (pythonG…

      9609792

      pythongh-71339: Use new assertion methods in the http tests (pythonGH…

      c3ce756

      pythongh-71339: Use new assertion methods in the urllib tests (python…

      a5811f6
    17. serhiy-storchaka
      added 4 commits that reference this issue on May 22, 2025

      [3.13] gh-71339: Use new assertion methods in the email tests (GH-129055

      34d3495

      [3.13] gh-71339: Use new assertion methods in the urllib tests (GH-12…

      6126b7c

      [3.13] gh-71339: Use new assertion methods in the http tests (GH-129058…

      ba9f533

      gh-71339: Use new assertion methods in tests (GH-129046)

      2602d8a
    18. miss-islington
      added a commit that references this issue on May 22, 2025

      pythongh-71339: Use new assertion methods in tests (pythonGH-129046)

      1b9bb43
    19. serhiy-storchaka
      closed this as completedon May 22, 2025
    20. github-project-automation
      moved this to Done in Unittest & doctest issueson May 22, 2025
    21. serhiy-storchaka
      added a commit that references this issue on May 22, 2025

      [3.14] gh-71339: Use new assertion methods in tests (GH-129046) (GH-1…

      db98e0b
    22. lkollar
      added a commit that references this issue on May 26, 2025

      pythongh-71339: Use new assertion methods in tests (pythonGH-129046)

      09a063d
    23. Pranjal095
      added a commit that references this issue on Jul 12, 2025

      pythongh-71339: Use new assertion methods in tests (pythonGH-129046)

      90c73e9
    24. taegyunkim
      added a commit that references this issue on Aug 4, 2025

      pythongh-71339: Use new assertion methods in tests (pythonGH-129046)

      e557711
    25. gostak-dd
      added a commit that references this issue on Jun 2, 2026

      pythongh-71339: Use new assertion methods in tests (pythonGH-129046)

      a20a70d
    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

      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytestsTests in the Lib/test dirTests in the Lib/test dirtype-featureA feature request or enhancementA feature request or enhancement

      Projects

      • Unittest & doctest issues
        Status
        Done
        Show more project fields

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.