Skip to content

unittest loaders should avoid loading test cases that are abstract base classes #120665

Description

@blhsing

Bug report

Bug description:

As reported in Discourse, loading a TestCase-derived subclass that is also an abstract base class like the following:

import unittest
from abc import ABC, abstractmethod

class FooBase(unittest.TestCase, ABC):
    @abstractmethod
    def test(self): ...

class Foo(FooBase):
    def test(self): pass

unittest.main()

would currently result in:

TypeError: Can't instantiate abstract class FooBase without an implementation for abstract method 'test'

We should make the unittest loaders avoid loading such test cases and methods from them.

CPython versions tested on:

3.12, CPython main branch

Operating systems tested on:

Linux, Windows

Linked PRs

Activity

  1. added 3 commits that reference this issue on Jun 18, 2024
  2. serhiy-storchaka commented on Jun 18, 2024

    @serhiy-storchaka
    Member

    See also previous discussion in #58739.

  3. JonathonReinhart commented on Jun 15, 2026

    @JonathonReinhart
    Contributor

    Until this issue is resolved, I hope this little snippet helps someone:

    import inspect
    import unittest
    
    # ...
    
    class MyTestLoader(unittest.TestLoader):
        def loadTestsFromTestCase(self, testCaseClass):
            if inspect.isabstract(testCaseClass):
                return self.suiteClass()
            return super().loadTestsFromTestCase(testCaseClass)
    
    
    if __name__ == "__main__":
        unittest.main(verbosity=2, testLoader=MyTestLoader())
  4. added a commit that references this issue on Jun 17, 2026
  5. added 3 commits that reference this issue on Jun 17, 2026
  6. added a commit that references this issue on Jun 17, 2026
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

    testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions