How to import custom mockery module? (import error: no module named my_module!)

I want to unit test my class, which is in another file called client_blogger.py .

My unit test file is in the same directory. All my other unit tests work, unless I try to make fun of one of my own methods.

 ## unit_test_client_blogger.py import mock import json from client_blogger import BloggerClient, requests Class TestProperties(): @pytest.fixture def blog(self): return BloggerClient(api_key='123', url='http://example.com') @mock.patch('client_blogger._jload') @mock.patch('client_blogger._send_request') def test_gets_blog_info(self, mock_send, mock_jload): """ Get valid blog info from API response. """ valid_blog_info = 'some valid json api response here' parsed_response = json.loads(valid_blog_info) correct_blog_id = '7488272653173849119' mock_jload.return_value = valid_blog_info id = self.blog().get_blog_info(parsed_response) assert id == correct_blog_id 

And here is the contents of the client_blogger.py file:

 # client_blogger.py import requests, json class BloggerClient(object): """ Client interface for Blogger API. """ def __init__(self, key, url): # removed some code here for brevity def _send_request(self, api_request): """ Sends an HTTP get request to Blogger API. Returns HTTP response in text format. """ # snip def _jload(self, api_response): """ Accepts text API response. Returns JSON encoded response. """ # snip def get_blog_info(self): """ Makes an API request. Returns Blog item information. """ request = '{b}/blogs/byurl?url={u}&key={k}'.format(b=self.base, u=self.url, k=self.key) txt_response = self.send_request(request) response = self._jload(txt_response) return response['id'] 

I want to mock the calls to the self.send_request() and self._jload() method in the above method.

But the Mock module complains: ImportError: No module named client_blogger .

The error should be here:

 @mock.patch('client_blogger._jload') @mock.patch('client_blogger._send_request') 

I tried many options to get mock.patch to find my module or class. But none of them worked.

I tried the following:

 @mock.patch('client_blogger.BloggerClient._jload') @mock.patch('BloggerClient._jload') @mock.patch('._jload') 

None of these works. Any idea how the mock.patch method is from my own module?

(It seems strange, because I can mock.patch other modules, just not my own: -s)

+12
python module mocking
source share
2 answers

Do you want to:

 @mock.patch('client_blogger.BloggerClient._jload') @mock.patch('client_blogger.BloggerClient._send_request') def test_gets_blog_info(self, mock_send, mock_jload): """ Get valid blog info from API response. """ valid_blog_info = 'some valid json api response here' parsed_response = json.loads(valid_blog_info) correct_blog_id = '7488272653173849119' mock_jload.return_value = valid_blog_info id = self.blog().get_blog_info(parsed_response) assert id == correct_blog_id 

The implementation of BloggerClient comes from the client_blogger module, so you need to schedule client_blogger.BloggerClient . You list this as one of the things you tried that don't work, but I just tried, and it works great for me. What was your question when you tried it?

+13
source share

For python3, the format is as follows:

 from unittest.mock import patch @patch('client_blogger.BloggerClient._jload') . . . 

Docs: https://docs.python.org/3/library/unittest.mock.html#patch

This is very, very important:

patch () is easy to use. The key is to make the correction in the correct namespace. See the section where to patch.

0
source share

All Articles