-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
gh-71339: Add additional assertion methods for unittest #128707
gh-71339: Add additional assertion methods for unittest #128707
Conversation
Add the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartswith() and assertNotStartswith() * assertEndswith() and assertNotEndswith() Also improve error messages for assertIsInstance() and assertNotIsInstance().
Great!
I recommend It doesn't really matter that (And we already have |
I wrote them initially as |
I created a poll: https://discuss.python.org/t/assertstartwith-vs-assertstartwith/76701. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good but I agree with Hugo that the name should be assertStartsWith.
Looks like the poll is pretty unanimous :-) The changes look great, but I'll admit I'm unlikely to use them as I tend to avoid the |
Co-authored-by: Hugo van Kemenade <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Just a few minor coding style remarks.
Lib/unittest/case.py
Outdated
@@ -1321,15 +1321,77 @@ def assertIsInstance(self, obj, cls, msg=None): | |||
"""Same as self.assertTrue(isinstance(obj, cls)), with a nicer | |||
default message.""" | |||
if not isinstance(obj, cls): | |||
standardMsg = '%s is not an instance of %r' % (safe_repr(obj), cls) | |||
if isinstance(cls, tuple): | |||
standardMsg = '%s is not an instance of any of %r' % (safe_repr(obj), cls) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may use f-strings :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, '%s is not an instance of any of %r' % (safe_repr(obj), cls)
and f'{safe_repr(obj)!s} is not an instance of any of {cls!r}'
produce the same bytecode. So the difference is only in readability, which is at large part subjective. I was not sure that inlining expressions in f-strings would make the code more readable, but if you think so...
Lib/unittest/case.py
Outdated
standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls) | ||
self.fail(self._formatMessage(msg, standardMsg)) | ||
|
||
def assertIsSubclass(self, cls, superclass, msg=None): | ||
try: | ||
r = issubclass(cls, superclass) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may avoid variables of a single letter: use "res" or "result". Same remark for new functions below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some docs nits and LGTM. I'm very happy to have assertHasAttr
because I needed it a lot in my personal projects.
Nice additions, thanks @serhiy-storchaka. |
…honGH-128707) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit 06cad77)
GH-128815 is a backport of this pull request to the 3.13 branch. |
…honGH-128707) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit 06cad77)
…-128707) (GH-128815) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit 06cad77)
…t.support (pythonGH-128707) (pythonGH-128815) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit c6a566e) Co-authored-by: Serhiy Storchaka <[email protected]> (cherry picked from commit 06cad77)
…-128707) (GH-128815) (GH-129059) Add a mix-in class ExtraAssertions containing the following methods: * assertHasAttr() and assertNotHasAttr() * assertIsSubclass() and assertNotIsSubclass() * assertStartsWith() and assertNotStartsWith() * assertEndsWith() and assertNotEndsWith() (cherry picked from commit c6a566e) Co-authored-by: Serhiy Storchaka <[email protected]> (cherry picked from commit 06cad77)
Add the following methods:
Also improve error messages for assertIsInstance() and assertNotIsInstance().
📚 Documentation preview 📚: https://cpython-previews--128707.org.readthedocs.build/