python-mock

Any way to reset a mocked method to its original state? - Python Mock - mock 1.0b1

让人想犯罪 __ 提交于 2020-12-27 12:44:07
问题 I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #... def check(self): #code... value = self.A() #more code... In my first test I mock only the method A from django.test import TestCase from mock import MagicMock import myClass class FirstTest(TestCase): def setUp(self): myClass.A = MagicMock(return_value = 'CPU') def test(self): #some tests myClassObj = myClass() myClassObj.check() Whereas in my second test I mock the entire check method: from

How do I mock the hierarchy of non-existing modules?

别说谁变了你拦得住时间么 提交于 2020-07-05 07:07:02
问题 Let's assume that we have a system of modules that exists only on production stage. At the moment of testing these modules do not exist. But still I would like to write tests for the code that uses those modules. Let's also assume that I know how to mock all the necessary objects from those modules. The question is: how do I conveniently add module stubs into current hierarchy? Here is a small example. The functionality I want to test is placed in a file called actual.py : actual.py: def

How do I mock the hierarchy of non-existing modules?

☆樱花仙子☆ 提交于 2020-07-05 07:06:18
问题 Let's assume that we have a system of modules that exists only on production stage. At the moment of testing these modules do not exist. But still I would like to write tests for the code that uses those modules. Let's also assume that I know how to mock all the necessary objects from those modules. The question is: how do I conveniently add module stubs into current hierarchy? Here is a small example. The functionality I want to test is placed in a file called actual.py : actual.py: def

How do I patch an object so that all methods are mocked except one?

痴心易碎 提交于 2020-05-11 04:00:10
问题 I have an entry point function call it main on an object that I would like to remain unmocked, since it calls several other methods on the object: class Thing(object): def main(self): self.alpha() self.bravo() def alpha(self): self.charlie() def bravo(self): raise TypeError("Requires Internet connection!") def charlie(self): raise Exception("Bad stuff happens here!") This is pretty straight forward to mock manually: thing = Thing() thing.alpha = MagicMock() thing.bravo = MagicMock() And I can

Python mock multiple return values

泪湿孤枕 提交于 2020-01-09 04:10:21
问题 I am using pythons mock.patch and would like to change the return value for each call. Here is the caveat: the function being patched has no inputs, so I can not change the return value based on the input. Here is my code for reference. def get_boolean_response(): response = io.prompt('y/n').lower() while response not in ('y', 'n', 'yes', 'no'): io.echo('Not a valid input. Try again']) response = io.prompt('y/n').lower() return response in ('y', 'yes') My Test code: @mock.patch('io') def test

mock open for unit testing in python

 ̄綄美尐妖づ 提交于 2020-01-03 19:06:10
问题 I would like to test, using unittest, a method which reads from a file using a context manager: with open(k_file, 'r') as content_file: content = content_file.read() I don't want to have to create a file on my system so I wanted to mock it, but I'm not suceeding much at the moment. I've found mock_open but I don't really understand how I'm supposed to use it and feed the mock as content_file in my test case. There is for instance this post here, but I do not understand how one is supposed to

Python: Mock Patch Errors with Flask

时间秒杀一切 提交于 2019-12-31 04:18:07
问题 I am a complete newb when it comes to writing Python, let alone testing it. Here is my Flask endpoint: @blueprint.route('/mailing_finish/<account_id>/<sumall_stream_id>/', methods=['POST']) def mailing_finish(account_id, sumall_stream_id): """ Get Response for mailing_id and update Dataset: * MYEMMA_EMAIL_SENDS: response['sent'] """ # TODO: webhook does not fire data = json.loads(request.data)['data'] access_token = sumall_redis.get_oauth_token(account_id) response_data = sumall_audience.get

changing the side effect of a mock object's method created with patch

无人久伴 提交于 2019-12-25 07:14:51
问题 Hello I want to test a view of django. Inside of the view I create two objects which I want to mock some of their methods. The test looks like this @mock.patch('payments_system.views.FirstObject') @mock.patch('payments_system.helper_functions.SecondObject') def test_make_payment_view_with_success(self, MockFirstObject, MockSecondObject): MockFirstObject.get_value.side_effect = get_value_side_effect //this function is defined and implemented outside my testcase class MockSecondObject.is_valid