Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Patch over a function imported inside another function

In order to avoid a circular import, I've been forced to define a function that looks like:

# do_something.py  def do_it():     from .helpers import do_it_helper     # do stuff 

Now I'd like to be able to test this function, with do_it_helper patched over. If the import were a top level import,

class Test_do_it(unittest.TestCase):     def test_do_it(self):         with patch('do_something.do_it_helper') as helper_mock:             helper_mock.return_value = 12             # test things 

would work fine. However, the code above gives me:

AttributeError: <module 'do_something'> does not have the attribute 'do_it_helper' 

On a whim, I also tried changing the patch statement to:

with patch('do_something.do_it.do_it_helper') as helper_mock: 

But that produced a similar error. Is there any way to mock this function, given the fact that I'm forced into importing it within the function where it's used?

like image 250
Wilduck Avatar asked Mar 05 '14 15:03

Wilduck


People also ask

Can you import inside a function?

Most of the time if you can do something in a function, you can do it in global scope or in a class definition or vice versa. There's an exception for from ... import * , which works at global scope but not inside a function.

How do you mock a function from another function in Python?

If you set autospec=True then the mock will be created with a spec from the object being replaced. All attributes of the mock will also have the spec of the corresponding attribute of the object being replaced. ... So, you need just describe expected data. Also you can mock API using httpretty.


1 Answers

You should mock out helpers.do_it_helper:

class Test_do_it(unittest.TestCase):     def test_do_it(self):         with patch('helpers.do_it_helper') as helper_mock:             helper_mock.return_value = 12             # test things 

Here's an example using mock on os.getcwd():

import unittest from mock import patch   def get_cwd():     from os import getcwd     return getcwd()   class MyTestCase(unittest.TestCase):     @patch('os.getcwd')     def test_mocked(self, mock_function):         mock_function.return_value = 'test'         self.assertEqual(get_cwd(), 'test') 

Hope that helps.

like image 82
alecxe Avatar answered Sep 29 '22 05:09

alecxe