Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using mock to patch a non-existing attribute

I'm trying to test a context manager that makes use of a class that uses some __getattr__ magic to resolve several attributes which don't actually exist on the class. I'm running into a problem where mock is raising an AttributeError when trying to patch the class.

Simplified example of objects I want to patch.

class MyClass(object):
    def __getattr__(self, attr):
        if attr == 'myfunc':
            return lambda:return None
        raise AttributeError('error')


class MyContextManager(object):
    def __init__(self):
        super(MyContextManager, self).__init__()
        self.myclass = MyClass()

    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.myclass.myfunc()

Test code

def test_MyContextManager():
    with patch.object(MyClass, 'myfunc', return_value=None) as mock_obj:
        with MyContextManager():
             pass

    # Do some tests on mock object

Here is the error I get:

AttributeError: <class 'MyClass'> does not have the attribute 'myfunc'

I'm able to do this and run the test, but it doesn't restore the attribute (or simply remove the mock attribute in this case) automatically:

MyClass.myfunc= Mock(return_value=None)

I'm open to using another library besides mock to achieve this. I'm also using pytest.

like image 667
Brendan Abel Avatar asked Apr 29 '15 18:04

Brendan Abel


Video Answer


1 Answers

To use patch in these kind of tests you should use create parameter that will force to create the attribute if not exist.

So your test should do something like this:

def test_MyContextManager():
    with patch.object(MyClass, 'myfunc', create=True, return_value=None) as mock_obj:
        with MyContextManager():
             pass
like image 93
Michele d'Amico Avatar answered Oct 10 '22 19:10

Michele d'Amico