Questions tagged [magicmock]

MagicMock is a subclass of Mock with default implementations of most of the magic methods.

MagicMock is a subclass of Mock with default implementations of most of the magic methods.

You can use MagicMock without having to configure the magic methods yourself.

163 questions
0
votes
1 answer

How do you set different attributes for different MagicMock instances in Python?

I'd like to assert that say_name was called with each value in the list name. from unittest.mock import patch class MyClass: def __init__(self, name): self.name = name def say_name(name): print('My name is ' + name) def…
Jacob Beauchamp
  • 532
  • 5
  • 18
0
votes
1 answer

How to set value on MagicMock obj. so that function under test doesn't return MagicMock object

Hey I'm just getting started with unittests and mocks in Python. Trying to test a function that takes a single column dataframe and returns a float after some calculation based on the dataframe values. import unittest from unittest.mock import…
0
votes
1 answer

How can I mock the following python function using MagicMock?

class C: def f(): calls g def g(): # Do something How can I mock g to test f in a testing module which imports class C?
Abhishek Kumar
  • 729
  • 6
  • 20
0
votes
1 answer

Proper checking MagicMock objects in Python unittests

I have this code under test: def to_be_tested(x): return round((x.a + x.b).c()) In my unittest I want to assert that exactly this is done with the passed x and the result returned, so I pass a MagicMock object as x: class…
Alfe
  • 56,346
  • 20
  • 107
  • 159
0
votes
2 answers

Mock object used in parent class

I have two classes in separate packages, one of them inherit from the other. I would like to test the child class. So how can I mock the external objects used in the parent class? I am confused in which namespace they reside at this point.
silentnights
  • 813
  • 3
  • 11
  • 21
0
votes
1 answer

Python unittest test passed in class method is called with

I have a method that takes two inputs a emailer class and a dict of data. def send_email(data, email_client): **** various data checks and formatting ***** response_code = email_client.create_email(recipient=receipient …
user7692855
  • 1,582
  • 5
  • 19
  • 39
0
votes
1 answer

Testing methods are called on object created

I'm new to Python so forgive me if this is basic. I have a method under test and in that method, I instantiate an object and call methods on it and want to test that those are called correctly (worth pointing out that this code is pre-existing and…
ediblecode
  • 11,701
  • 19
  • 68
  • 116
0
votes
1 answer

Configure return value mock with decorator only

Is there a way to capture the following logic in the patch decorator instead of having to pass the mock into the function: @patch('boto3.client') def test_playing_with_saml(self, boto3_client): boto3_client.return_value.assume_role_with_saml =…
RNikoopour
  • 523
  • 4
  • 16
0
votes
2 answers

How to mock multiple return values in Easymock

I am a python newbie and am trying to mock process.communicate method, but i do not know how to return multiple values from mock. The way i am approaching it is with patch.object(subprocess, 'Popen', new_callable=MagicMock) as process: …
Ankit Dixit
  • 142
  • 11
0
votes
2 answers

pytest: Mock returning a mock with specific properties

I found I often use same pattern in tests again and again: mock_get_data = mock.MagicMock() mock_get_data.get_data.return_value = "mocked DB data" mock_db = mock.Mock(spec=DBClass, return_value=mock_get_data) It would be used for testing (with…
George Shuklin
  • 6,952
  • 10
  • 39
  • 80
0
votes
1 answer

Python mocking a parameter

I have some code which invokes a HTTP request and I would like to unit test a negative case where it should raise a specific exception for a 404 response. However I am trying to figure out how to mock the parameter so it can raise the HTTPError as a…
Overly Excessive
  • 2,095
  • 16
  • 31
0
votes
1 answer

When patching 2 objects, second return first patched value

I am writing UT for one of my function where I have to patch 2 objects. @patch('mypackage.models.db_models.MongoClient', return_value={}) @patch('mypackage.models.db_models.GridFS') def test_file_in_db(self, mock_mongoclient, mock_gridfs): …
Nilesh
  • 20,521
  • 16
  • 92
  • 148
0
votes
1 answer

Can a Python mock object return the params it was given?

I am testing a Python method which makes an HTTP request to a server. The method looks like this: def do_something(): data = get_data() make_request(data) def make_request(self, data): self.send_response(200) …
user3470960
0
votes
1 answer

Mocking a variable in python

I am trying to mock a variable that represents the a state of a device In this case I have am trying to add a device and I have the following code: if self.network.controller.add_node( secure ) : for i in range( 0, 60 ) : if flagStarted…
Killercode
  • 894
  • 1
  • 20
  • 34
0
votes
1 answer

Partially mock a method in unittest `setUp`

I am trying to understand the mock/patch framework, but have a problem. Here are my simplified codes: file_a.py class A: def f(self): b = B() b.g() b.h() file_b.py class B: def g(self): return network_requests(...) def…
AoZ
  • 139
  • 1
  • 3
  • 10
1 2 3
10
11