Unittest: ignore test cases that are also ABC

The isabstract check proposed above would fix this, right? Since “TestCase cls can be properly created” if and only if “isabstract(cls)”, so it shouldn’t break any code that didn’t previously fail on test collection.

(I guess unless people start doing really weird stuff with custom AbstractMeta meta-sub-classes [1]. I think breaking those is ok)


  1. sub-meta-classes? Both sound weird… ↩︎

1 Like

I often had to use mixins to group test methods that can run on different implementations of the same interface, for example:
[…]

Did you try:

class BackendTestsMixin:
    backend: BackendInterface
    def test_read(self):
        assert isinstance(self, TestCase)
        self.assertEqual(self.backend.read(), "42")

That’s a very good point, and one that should classify the current behavior as a bug.

I’ve created a PR for this issue then.

1 Like

The only thing that object.__new__ looks at in determining whether a class is abstract and unable to be instantiated is with the Py_TPFLAGS_IS_ABSTRACT flag:

So it really doesn’t matter if a user makes a class abstract with a custom base class/metaclass as long as the flag is properly set.

1 Like

Ah, I see that it was previously discussed to add a new class decorator that would explicitly mark a test case as a base class so the loader would skip it. That certainly would work too, and would allow for other forms of base classes not limited to just abstract base classes with non-overridden abstract methods.

The only minor downside is that the “this is a base class” decorator feels redundant when the base class is clearly abstract already. Perhaps we can do both? That is, the loader would skip a test case if it’s abstract or marked as a base class by the decorator.

If we implement only the decorator we would then have to document that abstract classes must be marked by this decorator, or the resulting TypeError from running the module loader would still be considered a bug.

1 Like

I’m pleased to see that, on the 2-year anniversary of this thread, this bug has been resolved:

4 Likes

I missed that it was the exact date! :smiley: