Skip to content

gh-120665: make unittest loaders avoid loading test cases that are abstract base classes #120666

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

blhsing
Copy link
Contributor

@blhsing blhsing commented Jun 18, 2024

Make unittest loaders avoid loading test cases that are abstract base classes

Currently, 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'

This PR fixes the error by making the unittest loaders avoid loading such test cases and methods from them.

@clayg
Copy link

clayg commented Apr 4, 2025

@nedbat I think your comments on https://discuss.python.org/t/unittest-ignore-test-cases-that-are-also-abc/55958/14 suggest an understanding of how this would be a great QoL improvement for test authors. Please help it get merged upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants