python-mock

Python Mock with `from X import y`

瘦欲@ 提交于 2019-12-20 05:51:33
问题 I am trying to use Python's mock library in my unit testing but I am seeing inconsistent results depending on how I import the target that I am trying to patch. I would expect that both of these print statements should return False but it appears that only the second statement returns False : from requests import get import requests with mock.patch('requests.get') as get_mock: get_mock.return_value.ok = False print get('http://asdf.com').ok print requests.get('http://asdf.com').ok 回答1:

Mock an entire module in python

…衆ロ難τιáo~ 提交于 2019-12-19 16:53:32
问题 I have an application that imports a module from PyPI. I want to write unittests for that application's source code, but I do not want to use the module from PyPI in those tests. I want to mock it entirely (the testing machine will not contain that PyPI module, so any import will fail). Currently, each time I try to load the class I want to test in the unittests, I immediately get an import error. so I thought about maybe using try: except ImportError: and catch that import error, then use

Customizing unittest.mock.mock_open for iteration

和自甴很熟 提交于 2019-12-17 09:43:07
问题 How should I customize unittest.mock.mock_open to handle this code? file: impexpdemo.py def import_register(register_fn): with open(register_fn) as f: return [line for line in f] My first attempt tried read_data . class TestByteOrderMark1(unittest.TestCase): REGISTER_FN = 'test_dummy_path' TEST_TEXT = ['test text 1\n', 'test text 2\n'] def test_byte_order_mark_absent(self): m = unittest.mock.mock_open(read_data=self.TEST_TEXT) with unittest.mock.patch('builtins.open', m): result = impexpdemo

Python Mock object with method called multiple times

给你一囗甜甜゛ 提交于 2019-12-17 08:25:32
问题 I have a class that I'm testing which has as a dependency another class (an instance of which gets passed to the CUT's init method). I want to mock out this class using the Python Mock library. What I have is something like: mockobj = Mock(spec=MyDependencyClass) mockobj.methodfromdepclass.return_value = "the value I want the mock to return" assertTrue(mockobj.methodfromdepclass(42), "the value I want the mock to return") cutobj = ClassUnderTest(mockobj) Which is fine, but "methodfromdepclass

python-mock: 'self' parameter lacking default value

旧城冷巷雨未停 提交于 2019-12-13 16:09:45
问题 This used to work with python mock version 1.0.1, but started failing after I upgraded to mock version 1.3.0. I am running python version 2.7.10 on Mac OS X Yosemite 10.10.5. I reduced the logic from an existing production test to the following dummy test that reproduces the issue: import unittest import mock from mock import Mock, patch class Outer(object): class MyClass(object): def doStuff(self, action): pass @patch.object(Outer, "MyClass", autospec=True, return_value=Mock(spec_set=Outer

python mock what is return_value in the following

倾然丶 夕夏残阳落幕 提交于 2019-12-13 09:39:22
问题 i am very new to python mock and so just trying to understand the same. In the below code what is the difference between 1 and 2 statements indicated below,because in the end i can set mock_response.status_code with either of the statements import requests def get_data(): response = requests.get('https://www.somesite.com') return response.status_code if __name__ == '__main__': print get_data() Now what is the difference between the following codes, from call import get_data import unittest

Python 3 unittest patch doesn't return desired value

你。 提交于 2019-12-13 04:41:22
问题 I am trying to use the unitttest mock framework (python 3.4.9) to mock one of the method in my test case. And it's failing as it doesn't return the mocked value. This is the simplest example. In my case I can't change the way method being invoked. mock method def patch_this_method(): return 100 Test Case import unittest from unittest.mock import patch from libs.util import patch_this_method import libs class TestLibs(unittest.TestCase): @patch('libs.util.patch_this_method', return_value="200"

Mocking a RelatedManager in Django 2

做~自己de王妃 提交于 2019-12-12 16:30:29
问题 This question is directly related to this question, but that one is now outdated it seems. I am trying to test a view without having to access the database. To do that I need to Mock a RelatedManager on the user. I am using pytest and pytest-mock . models.py # truncated for brevity, taken from django-rest-knox class AuthToken(models.Model): user = models.ForeignKey( User, null=False, blank=False, related_name='auth_token_set', on_delete=models.CASCADE ) views.py class ChangeEmail(APIView):

Making a wrapper for `mock.patch`

ⅰ亾dé卋堺 提交于 2019-12-11 05:42:31
问题 Some customized patches from mock.patch I want to use over and over without littering my test code with copy-pastes of the patch setup. e.g. this very handy patch of datetime.date, which, adapted for datetime, would fill my code with with patch('mymodule.datetime') as mock_datetime: mock_datetime.datetime.utcnow.return_value = datetime.datetime(2010, 10, 8, 9, 10) mock_date.datetime.side_effect = lambda *args, **kw: datetime.datetime(*args, **kw) How can I wrap this functionality into a one

Need to mock out some base class behavior in a python test case

孤街浪徒 提交于 2019-12-11 05:17:55
问题 My title is fairly descriptive, but here goes. Suppose I have this setup. class BaseClass(object): def __init__(self): pass def base_function(self, param="Hello World"): print param #multiple inheritance probably irrelevant but my problem deals with it class DerivedClass(BaseClass, AnotherBaseClass): def __init__(self): pass def advanced_function(self): #blah blah blah #code code code self.base_function() Now, I have a situation where I am testing a derived class, but in doing so, I need to