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.

Title: Additional assert methods for unittest
Type: enhancement Stage: patch review
Components: Tests Versions: Python 3.6
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ChrisBarker, Pam.McANulty, ezio.melotti, michael.foord, r.david.murray, rbcollins, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-05-29 15:25 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin.

File name Uploaded Description Edit
extra_assertions.patch serhiy.storchaka, 2016-05-29 15:24 review
Messages (10)
msg266600 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-29 15:24
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
msg266601 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-29 15:29
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
msg266620 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-29 19:51
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.)
msg266627 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-29 20:20
I have this proposal (issue19645) in mind. We can add these method just in TestCase.

In any case I'm going to add the ExtraAssertions mixin in in maintained versions to help keeping branches in sync.
msg266675 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-05-30 05:37
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.
msg266682 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-30 06:58
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)).
msg267175 - (view) Author: Chris Barker (ChrisBarker) * Date: 2016-06-03 22:11
Why a mixin rather than adding to TestCase? If they are useful they should be easy to find.

Also, see Issue27198 for another possible new assert.
msg267185 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2016-06-03 23:21
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.

( and - sorry about the formatting in the blog post, wordpress changed theme details some time after I wrote the post and it now renders horribly :( )
msg268877 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-06-20 01:33
Ask five people to spell "assertEndsWith" and see how many of them capitalize the "W".
msg269074 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-06-22 14:03
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.
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71339
2016-06-22 14:03:21r.david.murraysetmessages: + msg269074
2016-06-20 01:33:47rhettingersetmessages: + msg268877
2016-06-19 22:43:56Pam.McANultysetnosy: + Pam.McANulty
2016-06-03 23:21:07rbcollinssetmessages: + msg267185
2016-06-03 22:11:15ChrisBarkersetnosy: + ChrisBarker
messages: + msg267175
2016-05-30 06:58:39serhiy.storchakasetmessages: + msg266682
2016-05-30 05:37:43rhettingersetnosy: + rhettinger
messages: + msg266675
2016-05-29 20:20:28serhiy.storchakasetmessages: + msg266627
2016-05-29 19:51:49r.david.murraysetmessages: + msg266620
2016-05-29 15:29:29serhiy.storchakasetmessages: + msg266601
2016-05-29 15:25:19serhiy.storchakacreate