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
2
votes
1 answer

What is difference between python mock and magic mock?

What is difference between python mock and magic mock, because I could see >>> dir(Mock()) == dir(MagicMock()) True when to use mock obj & when to use MagicMock obj??
Vb407
  • 1,577
  • 2
  • 15
  • 27
1
vote
1 answer

Mock the fetchone() method of the Mysql Cursor class and set its return value to None

I'm trying to make a MagicMock instance of the mysql connector, but I need for the method fetchone() to return None. This is what I've done so far: with mock.patch('mysql.connector.cursor') as dbmock, \ mock.patch('mysql.connector.connect',…
1
vote
0 answers

How to add args for mocked functions with MagicMock

I'm using MagicMock from unittest to mock an object in a class for unit tests. I am able to use the return_value to set return values for function calls but want to make it dynamic so that the response changes based on the args passed. Here's the…
Nik
  • 11
  • 1
1
vote
1 answer

Monkeypatching `__call__` on MagicMock

Let's say I define a helper method to monkeypatch a simple modification into the __call__ behavior of an existing object: def and_print_on_call(instance): class AndPrintOnCall(type(instance)): def __call__(self, *args, **kwarg): …
tlayton
  • 13
  • 3
1
vote
0 answers

My AWS lambda in python. It downloads a file from S3 and reads that file. Now I need to write a unittest mock test for that

I have an AWS lambda written in python. The lambda downloads a file from S3 to the folder /tmp/records. Then the lambda reads that file. Now I need to write a unit test for that. I need to mock the S3 call. I am wondering how to do that. Here is my…
1
vote
1 answer

Mock.patch returning MagicMock object causing AssertionError?

I have a function that I am trying to test in querySomething.py: class QuerySomething: def retrieveIssues(self,token): responses = [] if "customFields" in self._event: if not self.custom_fields: fields = [] …
Matt
  • 11
  • 4
1
vote
1 answer

MagicMock's reset_mock not properly resetting sub-mock's side_effect

I have a long-lived patch on a class, whose made instance undergoes multiple batches of assertions. Please see the below code snippet for the scenario. It exposes (what I think is annoying) behavior in MagicMock.reset_mock where it seemingly…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
1
vote
1 answer

How to do pytest to show method has been called with specified types of arguments?

My code is as below: class myClass: def __init__(self): self.client = myClient() def myMethod(self): query = Query() response = self.client.post(data=query) So now I'd like to introduce code change from data=query…
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
1
vote
1 answer

StopIteration when mocking base class of own class

In a rather complex test scenario I need to mock the base class of one of my own classes and instantiate the latter several times. When I do that my test errors with a StopIteration exception. Here's what my scenario boils down to in this…
1
vote
1 answer

python how to mock 3rd party library response

I'm using a 3rd party library "mailjet" to send email. Here's the doc: https://dev.mailjet.com/email/guides/getting-started/#prerequisites This is the method in send_email.py that I want to test: from mailjet_rest import Client def…
Emily
  • 137
  • 1
  • 2
  • 8
1
vote
2 answers

Why is function under test initializing class instead of using the mock in pytest?

I am writing a unit test for the following function, and I'm looking first at the case where we reach the last line. from azureml.core.authentication import InteractiveLoginAuthentication, ServicePrincipalAuthentication def get_authentication() ->…
dumbledad
  • 16,305
  • 23
  • 120
  • 273
1
vote
1 answer

pytest - mock exception of method to test Flask API

i'd like to unit test the following Flask API using pytest and mock: #api.py @api.route("/get_job/") class JobStatus(Resource): @api.marshal_with(m_res) def get(self, job_name): try: …
1
vote
0 answers

How to apply a decorator to a MagicMock function in unittest?

I am writing a test for a library using unittest. Real usage of the library would look like: from mylibrary import Connection con = Connection() @con.notification(int) def my_callback(value): print('New value:',…
Roberto
  • 958
  • 13
  • 33
1
vote
1 answer

How to mock an email confidential information in python

I have a method that returns a dictionary and that dictionary contains email specific confidential information like address,recipient, token etc. For sending an email, I need to access a rest URL and send the data along with confidential…
Rhea
  • 381
  • 1
  • 7
  • 22
1
vote
2 answers

How to instruct a magic mock on how it should treat its arguments

I've run into the following (edge?) case that I don't know how to handle properly. The general problem is that I have a function that I want to test in that function I call an external function with a generator comprehension as its argument in my…
Arne
  • 17,706
  • 5
  • 83
  • 99