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.

classification
Title: Add `assertIsSubclass` and `assertNotIsSubclass` to `unittest.TestCase`
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Brian.Jones, ezio.melotti, michael.foord, patricksmith, r.david.murray
Priority: normal Keywords: patch

Created on 2012-05-15 21:13 by patricksmith, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
assertissubclass.patch patricksmith, 2012-05-15 21:13 Patch that adds `assertIsSubclass` and `assertNotIsSubclass` to `unittest.TestCase` review
Messages (5)
msg160762 - (view) Author: Patrick Smith (patricksmith) Date: 2012-05-15 21:13
The attached patch adds two helper methods to `unittest.TestCase`: `assertIsSubclass` and `assertIsNotSubclass`. These methods are similar to the `assertIsInstance` and `assertIsNotInstance` methods that are already part of `unittest.TestCase`. They allow one to test if a class is a subclass of another class using the `issubclass` builtin.

These methods can be used like:
    self.assertIsSubclass(cls, parent_cls)

These new methods provide a nicer error message and more consistent interface over the alternatives:
    self.assertTrue(issubclass(cls, parent_cls))
    assert issubclass(cls, parent_cls)
msg160764 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-05-15 21:16
Hi, this has been discussed already, and we took the decision to leave these out, since checking for subclasses is not as common as checking the type.
If needed, this could still be implemented by the user, and possibly they could be added as recipes to the unittest doc.
Michael what do you think?
msg160844 - (view) Author: Brian Jones (Brian.Jones) * Date: 2012-05-16 12:17
I can't find a previous discussion of this topic. If you know the list it happened on, or the bug#, let me know as I'd be curious to see the discussion. 

While I could concede that checking type is arguably a more common case than checking ancestry, I think that checks like assertIsSubclass have a lot of value. 

First, if you view your collection of unit tests as pools of change detectors, this type of check is very valuable in order to detect changes in ancestry that result from a refactoring. 

Second, if you use a test-driven style of development, this is a very convenient method to have as your tests and code evolve, because the amount of code you have to write to create a failing test becomes a one-liner. 

As an aside, I *would* like to see the submitted patch provide more detail upon failure. Namely, if X is not a subclass of Y, it would be nice to know what it *is* a subclass of in the resulting output.
msg160875 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-05-16 15:02
I couldn't find any pointer to the discussion (maybe it happened on IRC), but the general goal is to provide only the most used/important assert methods and avoid bloating the API with less common ones.
msg160878 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-05-16 16:18
I agree that testing for subclass is a rather specialized thing, and thus should be defined by a project that needs it.  Normal API-centric unit tests shouldn't, IMO, care whether X is a subclass of Y.  If you are using it to write a failing unit test to "drive" refactoring from one class to another, that is, I think, a very good example of a specialized use case (one I would certainly never use, for example) and you should just put it in your own project test codebase.
History
Date User Action Args
2022-04-11 14:57:30adminsetgithub: 59024
2012-05-16 16:19:46r.david.murraysetstatus: open -> closed
2012-05-16 16:18:24r.david.murraysetstatus: pending -> open
nosy: + r.david.murray
messages: + msg160878

2012-05-16 15:02:54ezio.melottisetstatus: open -> pending

messages: + msg160875
2012-05-16 12:17:04Brian.Jonessetstatus: pending -> open
nosy: + Brian.Jones
messages: + msg160844

2012-05-15 21:16:57ezio.melottisetstatus: open -> pending

versions: + Python 3.3
nosy: + ezio.melotti, michael.foord

messages: + msg160764
resolution: rejected
stage: resolved
2012-05-15 21:13:14patricksmithcreate