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
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With