Python - Testing an abstract base class Python - Testing an abstract base class python python

Python - Testing an abstract base class


In newer versions of Python you can use unittest.mock.patch()

class MyAbcClassTest(unittest.TestCase):    @patch.multiple(MyAbcClass, __abstractmethods__=set())    def test(self):         self.instance = MyAbcClass() # Ha!


Here is what I have found: If you set __abstractmethods__ attribute to be an empty set you'll be able to instantiate abstract class. This behaviour is specified in PEP 3119:

If the resulting __abstractmethods__ set is non-empty, the class is considered abstract, and attempts to instantiate it will raise TypeError.

So you just need to clear this attribute for the duration of tests.

>>> import abc>>> class A(metaclass = abc.ABCMeta):...     @abc.abstractmethod...     def foo(self): pass

You cant instantiate A:

>>> A()Traceback (most recent call last):TypeError: Can't instantiate abstract class A with abstract methods foo

If you override __abstractmethods__ you can:

>>> A.__abstractmethods__=set()>>> A() #doctest: +ELLIPSIS<....A object at 0x...>

It works both ways:

>>> class B(object): pass>>> B() #doctest: +ELLIPSIS<....B object at 0x...>>>> B.__abstractmethods__={"foo"}>>> B()Traceback (most recent call last):TypeError: Can't instantiate abstract class B with abstract methods foo

You can also use unittest.mock (from 3.3) to override temporarily ABC behaviour.

>>> class A(metaclass = abc.ABCMeta):...     @abc.abstractmethod...     def foo(self): pass>>> from unittest.mock import patch>>> p = patch.multiple(A, __abstractmethods__=set())>>> p.start(){}>>> A() #doctest: +ELLIPSIS<....A object at 0x...>>>> p.stop()>>> A()Traceback (most recent call last):TypeError: Can't instantiate abstract class A with abstract methods foo


As properly put by lunaryon, it is not possible. The very purpose of ABCs containing abstract methods is that they are not instantiatable as declared.

However, it is possible to create a utility function that introspects an ABC, and creates a dummy, non abstract class on the fly. This function could be called directly inside your test method/function and spare you of having to wite boiler plate code on the test file just for testing a few methods.

def concreter(abclass):    """    >>> import abc    >>> class Abstract(metaclass=abc.ABCMeta):    ...     @abc.abstractmethod    ...     def bar(self):    ...        return None    >>> c = concreter(Abstract)    >>> c.__name__    'dummy_concrete_Abstract'    >>> c().bar() # doctest: +ELLIPSIS    (<abc_utils.Abstract object at 0x...>, (), {})    """    if not "__abstractmethods__" in abclass.__dict__:        return abclass    new_dict = abclass.__dict__.copy()    for abstractmethod in abclass.__abstractmethods__:        #replace each abc method or property with an identity function:        new_dict[abstractmethod] = lambda x, *args, **kw: (x, args, kw)    #creates a new class, with the overriden ABCs:    return type("dummy_concrete_%s" % abclass.__name__, (abclass,), new_dict)