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
1
vote
1 answer

unbound method f() must be called with x instance as first argument (got str instance instead)

Here is my class: class GoogleCloudLayer: def deleteMachine(self, machineName): return machineName + ' is dead. (stubbed)' It works: >>> gc = GoogleCloudLayer() >>> gc.deleteMachine('test') test is dead (stubbed) But I want to use in…
WebQube
  • 8,510
  • 12
  • 51
  • 93
1
vote
1 answer

Mocking a function that is invoked twice with different arguments

Suppose I have the following function: def function_to_test(context): list_of_invocations = [ func('arg%s' % number) for number in [1,2]] email_one = list_of_invocations[0].render(context) email_two =…
1
vote
2 answers

Mock map_async function argument yields PicklingError

Attempting to write some unittests around a function which performs a map_async() operation. More specifically, I want to confirm that some files get cleaned up in the event of an Exception occurring in one of the processes. Sample pseudo-code with…
Brent Hronik
  • 2,357
  • 1
  • 27
  • 43
1
vote
1 answer

How to mock this unit test in Python?

This is the method I want to test. In this test method (test_get_all_products), I want to pass in a list of products and a mocked response for the DB call which is identified by dal. def get_all_user_standalone_products(all_products,…
DLS
  • 5,313
  • 8
  • 37
  • 50
1
vote
1 answer

Python unittest mock: mocking a class function via a parameterized class object?

In lib/thing.py: class Class(object): def class_function1(self): In app/thing.py: def function2(class_object): class_object.class_function1() In test/test_thing.py, I want to patch lib.thing.Class.class_function1 when function2 is called…
kiminoa
  • 2,649
  • 3
  • 16
  • 17
1
vote
1 answer

How to unset mock.MagicMock in module level?

Suppose someone set the module as MagicMock in the head of the python file: sys.modules['moduleABC'] = mock.MagicMock() This cause trouble as moduleABC will be a mock when I try to run a whole list of unit test. How can I unset this to an actual…
Kintarō
  • 2,947
  • 10
  • 48
  • 75
1
vote
2 answers

How to MagicMock a list of object with method and get assert count

I am new to unit testing in python with MagicMock. I have the following code to assert the correct method count in python: def methodFoo(self): for booObject in self.booObjectList: booObject.shooMethod() I wish to perform an…
harsh8888
  • 477
  • 9
  • 22
0
votes
1 answer

Pytest patching a Validator of an Annotated type used in Pytantic Settings

Is there a way to patch a method used in a BeforeValidator of an Annotated type (used in Pydantic BaseSettings) such that the following code will work? Why doesn't it work? Is it because Annotated type is constructed first and then the patching is…
mrc
  • 126
  • 1
  • 11
0
votes
0 answers

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 MagicMock in test_example.py…
Ni3dzwi3dz
  • 177
  • 8
0
votes
0 answers

How can I use mocking to set each item in a list to whatever I want?

Here is my function that I want to write a unit test for: def check_files_content(files_spec): for item in files_specs: do_something .... Here is the unit test: def test_check_files_content(): …
ifrj
  • 91
  • 7
0
votes
1 answer

How can I mock the return value of a function to be a string, then mock out the return value of an attribute of that function?

So I am working with the below basic function: def whatever(..): for input_file in files: compressed_file = Path(input_file) if not compressed_file.name.replace('test_', '').startswith(COMPRESSED_FILE_PREFIX): continue So I…
ifrj
  • 91
  • 7
0
votes
1 answer

SQAlchemy .filter does not work when Session is in MagicMock()

I have a pytest.fixture for a mock database session that is used in a dependencies_overrides whenever a database session is triggered app.dependency_overrides[dependencies.get_db] = lambda: mock_db_session @pytest.fixture def mock_db_session(): …
0
votes
0 answers

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 callable(f): return…
Vishnuraj
  • 11
  • 2
0
votes
0 answers

How to mock SQLAlchemy's limit and offset functions using MagicMock?

I am trying to write a unit test for SQLAlchemy's Limit Offset functions. I am new to Python unit testing and I am not sure how to mock the session.query(table).limit(20).offset(20) part. I use unittest.mock.patch to mock the load_session function.…
user3595026
  • 560
  • 2
  • 9
  • 26
0
votes
1 answer

mock.patch-ing the .append method of the Python list

I am unit testing a single line of code within an if statement where I append an item onto a list if a variable has a specific value. foo = [] if bar == 'a': foo.append(bar) I would like to assert that such an append has been called. I have…
nerdenator
  • 1,265
  • 2
  • 18
  • 35