Skip to main content

All Questions

Filter by
Sorted by
Tagged with
1 vote
2 answers
109 views

MagickMock() assert_called not recognizing calls

i have a function like this, stored in file example.py: def my_func(file): conn = get_connection() conn.upload_file(file) conn.execute_file(file) Now, i want to test it, so i`m using ...
Ni3dzwi3dz's user avatar
0 votes
0 answers
41 views

unittest for decorator in python

class BackgroundProcess: def background(f): from functools import wraps @wraps(f) def wrapped(*args, **kwargs): loop = asyncio.get_event_loop() if ...
Vishnuraj's user avatar
1 vote
1 answer
430 views

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, ...
JuanKB1024's user avatar
1 vote
0 answers
71 views

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 ...
Nik's user avatar
  • 11
1 vote
1 answer
44 views

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)): ...
tlayton's user avatar
  • 13
3 votes
1 answer
873 views

Child Class from MagicMock object has weird spec='str' and can't use or mock methods of the class

When a class is created deriving from a MagicMock() object it has an unwanted spec='str'. Does anyone know why this happens? Does anyone know any operations that could be done to the MagicMock() ...
arrmansa's user avatar
  • 938
0 votes
0 answers
115 views

How do I mock a method that uses requests.get with the WITH keyword in my class?

I am having trouble understanding how mocking works when the get responses involve using the with keyword. Here is an example I am following for my class `Album' and I have been successful when I am ...
Lee's user avatar
  • 1
1 vote
0 answers
262 views

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 ...
srinin's user avatar
  • 83
0 votes
0 answers
101 views

Mocking method from another class with python unittest mock

I have a class and within that class there are used methods from another classes. Let's say it looks like that (just an example): from jobs.fruit import Fruit from jobs.veggie import Veggie class ...
nola's user avatar
  • 1
1 vote
1 answer
2k views

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 ...
Intrastellar Explorer's user avatar
0 votes
1 answer
202 views

How to magic mock long chained calls?

To test the following functino, I want to assert .filter() is called once with parameter filters. def get_instances(session: boto3.session.Session, filters): instances = session.resource('ec2')....
ca9163d9's user avatar
  • 29.1k
2 votes
1 answer
1k views

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 ...
Kai Roesner's user avatar
4 votes
0 answers
627 views

Python unittest returns MagicMock object instead of return value

I have a python which has a class A. Class A object has a method that takes a list as input and sends a post request to a client endpoint to create new sources in the database and returns a tuple of ...
NAB0815's user avatar
  • 471
0 votes
1 answer
2k views

python mocks a function properly, however the call count is not accounted

I have a class like the below: Class a: def fn1(self): p1=multiprocessing.Process(self.fn2, (arg1,) p1.start() p1.join() def fn2(self, arg1): … I am trying to ...
user1522160's user avatar