简体   繁体   中英

How to mock a builtin method

I have an object which defines a method which is accessible only if it's instantiated first:

>>> import select
>>> select.poll().poll
<built-in method poll of select.poll object at 0x7fcf2a94bd80>
>>> select.poll.poll
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'poll'
>>> 

As such I am having troubles mocking it (the "poll" method).

>>> from unittest import mock
>>> import select
>>> with mock.patch('select.poll.poll') as m:
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/unittest/mock.py", line 1197, in __enter__
    original, local = self.get_original()
  File "/usr/local/lib/python3.4/unittest/mock.py", line 1171, in get_original
    "%s does not have the attribute %r" % (target, name)
AttributeError: <built-in function poll> does not have the attribute 'poll'

Any recommendation?

As mentioned in the comments, you are better off mocking select.poll . You don't need to go further in to that module. You are testing the behaviour of an external module raising something to know how your code will react to it. Therefore, what you can do with keeping that in mind is this:

import unittest
import select

from mock import mock, Mock


def my_method():
    select.poll()


class Testor(unittest.TestCase):
    @mock.patch('select.poll', return_value=Mock())
    def test_it(self, m_poll):    
        m_poll.side_effect = Exception

        with self.assertRaises(Exception):
            my_method()

if __name__ == '__main__':
    unittest.main()

So, you mock patch select.poll , and then when you call poll() you will have a side_effect set to raise an exception. With the self.assertRaises to check if an exception does get raised when you call my_method .

Alternatively, if you are looking to really mock an additional method inside poll, you can do something like this:

import unittest
import select

from mock import mock, Mock


def my_method():
    s = select.poll()
    s.poll()


class Testor(unittest.TestCase):
    @mock.patch('select.poll', return_value=Mock())
    def test_it(self, m_poll):
        m_poll.return_value.poll.side_effect = Exception
        with self.assertRaises(Exception):
            my_method()

if __name__ == '__main__':
    unittest.main()

From the return value of m_poll.return_value , get the side_effect of poll to return your Exception .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM