Python mocking the right class/import
NickName:GreNait Ask DateTime:2021-03-09T22:35:19

Python mocking the right class/import

I am trying to implement unittests for my python program. The problem is, that my program is using several imported classes, which I would like to replace by a mocked object/class to verify single functions/methods.

I do not get any errors with my mocked class. But it appears that the mock itself didn't replace the object I wanted to replace.

This is basically my structure:

First the class I want to mock. Might look like that:

class ToMock():
    def getSomething(self):
        return "something"

The class I want to test looks like this:

from x.y import ToMock

class ClassToTest():
    def __init__(self):
        self.obj = ToMock()

    def returnStuff():
        return self.obj.getSomething()

As you can imagine, I want to test the returnStuff method. Therfore I want to mock .getSomething, or better said the whole ToMock object.

The unittest should therefore test the ClassToTest with the mocked ToMock class. I tried several mock.patch variants, but I couldn't get it to run/test properly.

import unittest
from unittest import mock

from a.b import ClassToTest

class TestObject(unittest.TestCase):
    def setUp(self):
        with mock.patch('x.y.ToMock') as mock_obj:
            mock_obj.return_value.getSomething().return_value="mocked return value"
            self.test_class = ClassToTest()
            result = self.test_class.returnStuff() # This should return now 'mocked return value', I guess?
            mock_obj.return_value.getSomething.assert_called_once_with("")

The problem I face is, that the self.test_class.returnStuff() is not "calling" the mocked object, but imports the real class etc. and therefore I am running into timeouts, or similar stuff.

I am sure, that I provide the wrong path for the object which should be mocked. Perhaps someone can hint me into the right direction.

Thanks -GreNait

Copyright Notice:Content Author:「GreNait」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66548920/python-mocking-the-right-class-import

More about “Python mocking the right class/import” related questions

Python mocking the right class/import

I am trying to implement unittests for my python program. The problem is, that my program is using several imported classes, which I would like to replace by a mocked object/class to verify single

Show Detail

python mocking function calls that are called during module import

I need to perform mocking for python code that is running during module import For example I have code like this import configparser config = configparser.ConfigParser() config.read('test.ini') ...

Show Detail

Mocking in Python From an Imported Class

I'm having a hard time mocking os.environ.get hoping you can help. I have a BaseClass that is imported. I'm trying to mock the os.environ.get call in that base_class file when import the BaseClass

Show Detail

Mocking functions with FlexMock in Python?

I know how to mock methods in Python using flexmock, like flexmock(subprocess).should_receive('call').replace_with(my_func) How does one mock functions outside objects, or for example glob, whic...

Show Detail

newbiee query: mocking in python

I'm trying to learn Python and mocking infrastructure in Python at the same time (Due to requirement at my work place). I should also mention that I'm also not familiar with mocking feature in C++ ...

Show Detail

Mocking class by replacing it

imagine you have a module from module.my_module import Foo def my_function(): foo = Foo() return whatewer_can_happen(foo) that you want to test like this class TestFoo: def __init__()...

Show Detail

Stubbing vs Mocking in Python

I'm want to mock or stub a function for testing. Not sure if I have the terminology right so correct me if I'm wrong, but I understand a mock as using a mocking library similar to unittest.mock to ...

Show Detail

Mocking A Class Returned By A Function In Python

I have a class hierarchy with a bunch of wrappers that I am trying to test. I have created a simple version of what I am trying to do below but cannot seem to figure out where my problem is. Class...

Show Detail

python class method mocking failure

Trying to understand mocking/patching and I have a restful API project with three files (FYI, I'm using flask) class1.py domain.py test_domain.py class1.py file content: class one: def addit...

Show Detail

Python - Mocking OS Error Exception

Trying to mock a PermissionError exception in Python 3.6 using side_effect. Looks like my function is called and the EPERM exception is raised, but then it fails to run my except statements. Same...

Show Detail