Search code examples
pythonpython-3.xpytest

pytest-mock: Mock an abstract class


I'm trying to make the following code work:

from pytest_mock import mocker

class TestClass(mocker):
  def setup_method(self):
    self.some_mock = SomeAbstractClass()
    self.testsubject = ClassThatIsBeingTested(self.some_mock)

  def test_1(self):
    mocker.patch(self.some_mock, 'some_function', return_value=5)
    assert 5 == self.testsubject.function_that_internally_uses_the_mock()

But I get a TypeError for trying to instantiate an abstract class.

How can I mock SomeAbstractClass?


Solution

  • Instead of trying to patch a real instance, you can use the unittest.mock module (also provided as the mock package for Python 2.7) to create a mock instance:

    import sys
    
    if sys.version_info[0] == 2:
        from mock import Mock
    else:
        from unittest.mock import Mock
    
    # ...
    
    some_mock = Mock(spec=SomeAbstractClass)
    some_mock.some_function.return_value = 5
    
    some_mock.some_function()
    # => 5
    
    some_mock.non_existent_function()
    # => AttributeError: Mock object has no attribute 'non_existent_function'
    

    This works for creating mock instances of any class—not just abstract ones.