vendredi 21 août 2015

Mock patch a function that doesn't exist on a class

Using Python's mock framework, is it possible to mock patch a function that doesn't exist on a class. If so, how?

For example:

example.py

import mock
import unittest


class MyClass(object):
    pass


class MyTests(unittest.TestCase):

    def test_mock_non_existent_function(self):
        with mock.patch('example.MyClass.my_function'):
            pass

Running that test raises an error:

Error
Traceback (most recent call last):
  File "/Users/jesse/Code/my_proj/lib/mock.py", line 1193, in patched
  File "/Users/jesse/Code/my_proj/lib/mock.py", line 1268, in __enter__
  File "/Users/jesse/Code/my_proj/lib/mock.py", line 1242, in get_original
AttributeError: <class 'example.MyClass'> does not have the attribute 'my_function'

Using Python 2.7.9 and mock 1.0.1.

Aucun commentaire:

Enregistrer un commentaire