magicmock

Python returns MagicMock object instead of return_value

强颜欢笑 提交于 2021-01-16 04:43:20
问题 I have a python file a.py which contains two classes A and B . class A(object): def method_a(self): return "Class A method a" class B(object): def method_b(self): a = A() print a.method_a() I would like to unittest method_b in class B by mocking A . Here is the content of the file testa.py for this purpose: import unittest import mock import a class TestB(unittest.TestCase): @mock.patch('a.A') def test_method_b(self, mock_a): mock_a.method_a.return_value = 'Mocked A' b = a.B() b.method_b() if

Mocking a imported function with pytest [duplicate]

二次信任 提交于 2020-01-23 12:42:13
问题 This question already has answers here : Python Mocking a function from an imported module (2 answers) Closed 2 years ago . I would like to test a email sending method I wrote. In file, format_email.py I import send_email. from cars.lib.email import send_email class CarEmails(object): def __init__(self, email_client, config): self.email_client = email_client self.config = config def send_cars_email(self, recipients, input_payload): After formatting the email content in send_cars_email() I

Mocking a imported function with pytest [duplicate]

放肆的年华 提交于 2020-01-23 12:41:33
问题 This question already has answers here : Python Mocking a function from an imported module (2 answers) Closed 2 years ago . I would like to test a email sending method I wrote. In file, format_email.py I import send_email. from cars.lib.email import send_email class CarEmails(object): def __init__(self, email_client, config): self.email_client = email_client self.config = config def send_cars_email(self, recipients, input_payload): After formatting the email content in send_cars_email() I

Python HTTP Post method returns response as magicmock object instead of value

白昼怎懂夜的黑 提交于 2020-01-04 06:36:09
问题 I am trying to check the response status code after trigerring some API with a POST method, Response status code is of Magicmock instance type, i am checking whether the status code is inbetween 400 and 500 using comparison operator which works in python 2 but raises TypeError in python 3 import mock response = <MagicMock name='Session().post()' id='130996186'> Below code works in python 2 if (400 <= response.status_code <= 500): print('works') But when executed in python 3, raises TypeError:

Python HTTP Post method returns response as magicmock object instead of value

*爱你&永不变心* 提交于 2020-01-04 06:35:39
问题 I am trying to check the response status code after trigerring some API with a POST method, Response status code is of Magicmock instance type, i am checking whether the status code is inbetween 400 and 500 using comparison operator which works in python 2 but raises TypeError in python 3 import mock response = <MagicMock name='Session().post()' id='130996186'> Below code works in python 2 if (400 <= response.status_code <= 500): print('works') But when executed in python 3, raises TypeError:

python check if a method is called without mocking it away

ぃ、小莉子 提交于 2019-12-22 04:56:11
问题 class A(): def tmp(self): print("hi") def b(a): a.tmp() To check if tmp method is called in b, the recommended way is a = A() a.tmp = MagicMock() b(a) a.tmp.assert_called() But tmp here is being mocked away and is not resulting in a "hi" getting printed. I would want my unit test to check if method tmp is called without mocking it away. Is this possible? I know this is not a standard thing to expect when writing unitests. But my use case (which is bit tricky) requires this. 回答1: You can set

Assert mocked function called with json string in python

烈酒焚心 提交于 2019-12-21 14:40:41
问题 Writing some unit tests in python and using MagicMock to mock out a method that accepts a JSON string as input. In my unit test, I want to assert that it is called with given arguments, however I run into issues with the assert statement, since the ordering of objects within the dict doesn't matter, besides in the assert statement for the string. Simplified example of what I am trying to achieve below. mock_funct = MagicMock() # mocked function called elsewhere expected = {"a":"a", "b":"b"}

Mocking Numpy Structured Arrays

杀马特。学长 韩版系。学妹 提交于 2019-12-13 06:11:43
问题 I'm trying to figure out how to mock a numpy structured array and am not having much luck. Ideally, I'd like to do something like this: from mock import MagicMock mock_obj = MagicMock() mock_obj['some']['test']['structure'] = 3 assert 3 == mock_obj['some']['test']['structure'] I understand how to mock a single dictionary using the side_effect but haven't figured out how to do it for arbitrary, nested __getitem__ or __setitem__ functions. EDIT: Here is some context: def function(self): arr =

python mock default init argument of class

微笑、不失礼 提交于 2019-12-11 02:38:52
问题 I want to mock the default argument in a class constructor: class A (object): def __init__(self, connection=DefaultConnection()): self.connection = connection I want to mock DefaultConnection in my unittests, but it doesn't work when passed in as a default value. 回答1: You can use patch to patch the module, and then you can set the return value as a Mock. # --- a.py (in package path x.y) -- from c import DefaultConnection class A (object): def __init__(self, connection=DefaultConnection()):

Magic mock assert_called_once vs assert_called_once_with weird behaviour

旧街凉风 提交于 2019-12-10 15:45:09
问题 I am noticing a weird behavior with assert_called_once and assert_called_once_with in python. This is my real simple test: File module/a.py from .b import B class A(object): def __init__(self): self.b = B("hi") def call_b_hello(self): print(self.b.hello()) File module/b.py class B(object): def __init__(self, string): print("created B") self.string = string; def hello(self): return self.string These are my tests: import unittest from mock import patch from module.a import A class MCVETests