Skip to content

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

Merged
nedbat merged 7 commits into
python:mainfrom
blhsing:fix-unittest-loader-abc-testcase
Jun 17, 2026
Merged

gh-120665: make unittest loaders avoid loading test cases that are abstract base classes#120666
nedbat merged 7 commits into
python:mainfrom
blhsing:fix-unittest-loader-abc-testcase

Conversation

@blhsing

@blhsing blhsing commented Jun 18, 2024

Copy link
Copy Markdown
Contributor

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.

Comment thread Lib/test/test_unittest/test_loader.py Outdated
@clayg

clayg commented Apr 4, 2025

Copy link
Copy Markdown

@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.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 17, 2026
@JonathonReinhart

Copy link
Copy Markdown
Contributor

This seems like such a common-sense enhancement, I'm surprised it's being debated so heavily.

The argument that this could break existing code is invalid; existing code that hits this path would already raise an exception and never run.

And the intent of class FooBase(unittest.TestCase, ABC): is extremely clear IMO.

Please consider landing this change.

@nedbat nedbat merged commit 5ad3c6d into python:main Jun 17, 2026
69 of 71 checks passed
@nedbat nedbat added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes and removed stale Stale PR or inactive for long period of time. labels Jun 17, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @blhsing for the PR, and @nedbat for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @blhsing for the PR, and @nedbat for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @blhsing for the PR, and @nedbat for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-app

bedevere-app Bot commented Jun 17, 2026

Copy link
Copy Markdown

GH-151599 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jun 17, 2026
@bedevere-app

bedevere-app Bot commented Jun 17, 2026

Copy link
Copy Markdown

GH-151600 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 17, 2026
@bedevere-app

bedevere-app Bot commented Jun 17, 2026

Copy link
Copy Markdown

GH-151601 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants