Skip to content
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

Add assertIsSubclass and assertNotIsSubclass to unittest.TestCase #59024

Closed
patricksmith mannequin opened this issue May 15, 2012 · 5 comments
Closed

Add assertIsSubclass and assertNotIsSubclass to unittest.TestCase #59024

patricksmith mannequin opened this issue May 15, 2012 · 5 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@patricksmith
Copy link
Mannequin

patricksmith mannequin commented May 15, 2012

BPO 14819
Nosy @ezio-melotti, @bitdancer, @voidspace
Files
  • assertissubclass.patch: Patch that adds assertIsSubclass and assertNotIsSubclass to unittest.TestCase
  • 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 = <Date 2012-05-16.16:19:46.601>
    created_at = <Date 2012-05-15.21:13:14.536>
    labels = ['type-feature', 'library']
    title = 'Add `assertIsSubclass` and `assertNotIsSubclass` to `unittest.TestCase`'
    updated_at = <Date 2012-05-16.16:19:46.600>
    user = 'https://bugs.python.org/patricksmith'

    bugs.python.org fields:

    activity = <Date 2012-05-16.16:19:46.600>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-05-16.16:19:46.601>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2012-05-15.21:13:14.536>
    creator = 'patricksmith'
    dependencies = []
    files = ['25600']
    hgrepos = []
    issue_num = 14819
    keywords = ['patch']
    message_count = 5.0
    messages = ['160762', '160764', '160844', '160875', '160878']
    nosy_count = 5.0
    nosy_names = ['ezio.melotti', 'r.david.murray', 'michael.foord', 'Brian.Jones', 'patricksmith']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue14819'
    versions = ['Python 3.3']

    @patricksmith
    Copy link
    Mannequin Author

    patricksmith mannequin commented May 15, 2012

    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)

    @patricksmith patricksmith mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 15, 2012
    @ezio-melotti
    Copy link
    Member

    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?

    @BrianJones
    Copy link
    Mannequin

    BrianJones mannequin commented May 16, 2012

    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.

    @ezio-melotti
    Copy link
    Member

    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.

    @bitdancer
    Copy link
    Member

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants