python-mock

How to mock a redis client in Python?

霸气de小男生 提交于 2019-12-06 17:41:08
问题 I just found that a bunch of unit tests are failing, due a developer hasn't mocked out the dependency to a redis client within the test. I'm trying to give a hand in this matter but have difficulties myself. The method writes to a redis client: redis_client = get_redis_client() redis_client.set('temp-facility-data', cPickle.dumps(df)) Later in the assert the result is retrieved: res = cPickle.loads(get_redis_client().get('temp-facility-data')) expected = pd.Series([set([1, 2, 3, 4, 5])],

Python mock: wrap instance method

折月煮酒 提交于 2019-12-06 06:54:36
问题 What I would like: Ensure that all instances of Foo that are created inside the with statement have their foo instance method wrapped in a MagicMock via wraps=Foo.foo . The reason I want this is so that I can track call_count on the method foo for all instances of Foo that are created. Now that I say it like that it seems kind of impossible... >>> from mock import patch ... ... class Foo(object): ... ... def foo(self): ... return "foo" ... ... with patch("__main__.Foo.foo", wraps=Foo.foo) as

Mocking default=timezone.now for unit tests

一个人想着一个人 提交于 2019-12-05 10:53:56
问题 I'm trying to write unit tests for a django app that does a lot of datetime operations. I have installed mock to monkey patch django's timezone.now for my tests. While I am able to successfully mock timezone.now when it is called normally (actually calling timezone.now() in my code, I am not able to mock it for models that are created with a DateTimeField with default=timezone.now . I have a User model that contains the following: from django.utils import timezone ... timestamp = models

Mocking ftplib.FTP for unit testing Python code

醉酒当歌 提交于 2019-12-05 01:21:02
I don't know why I'm just not getting this, but I want to use mock in Python to test that my functions are calling functions in ftplib.FTP correctly. I've simplified everything down and still am not wrapping my head around how it works. Here is a simple example: import unittest import ftplib from unittest.mock import patch def download_file(hostname, file_path, file_name): ftp = ftplib.FTP(hostname) ftp.login() ftp.cwd(file_path) class TestDownloader(unittest.TestCase): @patch('ftplib.FTP') def test_download_file(self, mock_ftp): download_file('ftp.server.local', 'pub/files', 'wanted_file.txt'

How to mock a redis client in Python?

萝らか妹 提交于 2019-12-04 23:22:16
I just found that a bunch of unit tests are failing, due a developer hasn't mocked out the dependency to a redis client within the test. I'm trying to give a hand in this matter but have difficulties myself. The method writes to a redis client: redis_client = get_redis_client() redis_client.set('temp-facility-data', cPickle.dumps(df)) Later in the assert the result is retrieved: res = cPickle.loads(get_redis_client().get('temp-facility-data')) expected = pd.Series([set([1, 2, 3, 4, 5])], index=[1]) assert_series_equal(res.variation_pks, expected) I managed to patch the redis client's get() and

Mocking file objects or iterables in python

南楼画角 提交于 2019-12-03 23:55:33
问题 Which way is proper for mocking and testing code that iters object returned by open(), using mock library? whitelist_data.py : WHITELIST_FILE = "testdata.txt" format_str = lambda s: s.rstrip().lstrip('www.') whitelist = None with open(WHITELIST_FILE) as whitelist_data: whitelist = set(format_str(line) for line in whitelist_data) if not whitelist: raise RuntimeError("Can't read data from %s file" % WHITELIST_FILE) def is_whitelisted(substr): return 1 if format_str(substr) in whitelist else 0

Mocking default=timezone.now for unit tests

微笑、不失礼 提交于 2019-12-03 23:03:08
I'm trying to write unit tests for a django app that does a lot of datetime operations. I have installed mock to monkey patch django's timezone.now for my tests. While I am able to successfully mock timezone.now when it is called normally (actually calling timezone.now() in my code, I am not able to mock it for models that are created with a DateTimeField with default=timezone.now . I have a User model that contains the following: from django.utils import timezone ... timestamp = models.DateTimeField(default=timezone.now) modified = models.DateTimeField(default=timezone.now) ... def save(self,

Mock patching from/import statement in Python

余生颓废 提交于 2019-12-03 08:26:24
问题 I am trying to get mock.patch to work on the following piece of sample code: from mock import patch from collections import defaultdict with patch('collections.defaultdict'): d = defaultdict() print 'd:', d This outputs the following: d: defaultdict(None, {}) Which means that defaultdict was not patched. If I replace the from/import statement with a straight import statement it works: from mock import patch import collections with patch('collections.defaultdict'): d = collections.defaultdict(

Mock patching from/import statement in Python

隐身守侯 提交于 2019-12-02 23:46:57
I am trying to get mock.patch to work on the following piece of sample code: from mock import patch from collections import defaultdict with patch('collections.defaultdict'): d = defaultdict() print 'd:', d This outputs the following: d: defaultdict(None, {}) Which means that defaultdict was not patched. If I replace the from/import statement with a straight import statement it works: from mock import patch import collections with patch('collections.defaultdict'): d = collections.defaultdict() print 'd:', d Output is: d: <MagicMock name='defaultdict()' id='139953944084176'> Is there any way to

Mocking only a single method on an object

*爱你&永不变心* 提交于 2019-12-01 13:42:23
问题 I'm familiar with other mocking libraries in other languages such as Mockito in Java, but Python's mock library confuses the life out of me. I have the following class which I would like to test. class MyClassUnderTest(object): def submethod(self, *args): do_dangerous_things() def main_method(self): self.submethod("Nothing.") In my tests, I'd like to make sure that the submethod was called when main_method was executed and that it was called with the right arguments. I don't want submethod to