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

python mock default init argument of class

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…
Atul Bhatia
  • 1,633
  • 5
  • 25
  • 50
2
votes
1 answer

Mocking asynchronous context manager for SQLAlchemy connection

I have a problem testing the code where I connect to the database via SQLAlchemy using my own asynchronous context manager. # my_module.py from contextlib import asynccontextmanager from typing import Any, AsyncGenerator from…
2
votes
1 answer

Pytest mocker.patch is returning NonCallableMagicMock

This test is driving me insane and I can't figure it out. mocker.patch is returning a MagicMock (as expected) in my actual tests. However, when it invokes the modules and the class that I wanted to patch is mocked, it is returning a…
SynackSA
  • 855
  • 1
  • 12
  • 35
2
votes
1 answer

How to check of exceptions in nested functions when using MagicMock objects in Pytest?

I've a function (myfunc), with a validation input a and and c credentials that will setup a service to call func_z. For some validation input, func_z will throw an error, and in other cases, it'll return some dictionary of values. And I've an…
alvas
  • 115,346
  • 109
  • 446
  • 738
2
votes
1 answer

Python Mock two functions in Pytest test using MagicMock

This is the function I am trying to test. def service_request(url, session=configure_session()): response = session.get(url) document = json.loads(response.text) return document Yes it is basic right now (hardly worth testing you might…
SarahJessica
  • 473
  • 1
  • 7
  • 18
2
votes
1 answer

Patch attribute of mocked class

I'm attempting to mock a class. The class I'm attempting to mock looks like the following (lines have been removed for brevity): class Connection(object): """Connection. """ def __init__(self, base_url=None, creds=None,…
a11smiles
  • 1,190
  • 1
  • 9
  • 21
2
votes
1 answer

MagicMock the response to get data in python 3.x

I have below two functions which I am trying to write unittest case using magicMock. import json from unittest import mock from unittest.mock import MagicMock def get_response(m, time): response = get_data(m, time) return response def…
user15051990
  • 1,835
  • 2
  • 28
  • 42
2
votes
1 answer

how to mock a file object in python3

In python2 I have this in my test method: mock_file = MagicMock(spec=file) I'm moving to python3, and I can't figure out how to do a similar mock. I've tried: from io import IOBase mock_file = MagicMock(spec=IOBase) mock_file =…
zli
  • 307
  • 1
  • 12
2
votes
0 answers

Patching a function in PySpark using MagicMock does not patch in the spark run

I have a unit-test (using PyTest) that runs my PySpark tests. I have the normal conftest.py that creates SQLContext. I would like to get the same uuid4 in all cases, so I patched uuid4 in my test. If I call uuid.uuid4() from the test funnction, all…
ronhash
  • 854
  • 7
  • 16
2
votes
2 answers

How to get MagicMock to return multiple values

I'm trying to mock a library (matplotlib for what it's worth), and am hitting an issue where it fails when the mock is called expecting a tuple returned. Is there a better way to do this? Python 3.7.2 (default, Jan 13 2019, 12:50:15) [Clang 10.0.0…
TinyTheBrontosaurus
  • 4,010
  • 6
  • 21
  • 34
2
votes
1 answer

How can I call a method on a copied MagicMock in Python?

I want test that a function has called the __copy__ method (with a given set of arguments) on an object that was passed to it. Since a DB connection is required to create the object, I thought I should mock it and pass the mock to the function for…
Jacob Beauchamp
  • 532
  • 5
  • 18
2
votes
1 answer

Assign a MagicMock instance to return_value of another MagicMock instance

I'm trying to mock the return value of a MagicMock instance's function, but the result doesn't go as I expected: >>> f = mock.MagicMock() # => >>> g = mock.MagicMock() # => >>>…
Lan Do
  • 63
  • 6
2
votes
1 answer

Unittest python method for return mocked Dataframe

I have a Python method called Pippo that during its execution it calls other methods which return the Dataframes to be processed. I want to mock every single method return with a custom Dataframe, but I can not understand how to patch them…
2
votes
1 answer

Unresolved reference MagicMock

I want to Mock a method in a Python Test. In order to do this I need MagicMock which I try to import it like this: from unittest import TestCase from unittest.mock import MagicMock Even after I clicked several times on install in PyCharm and…
John Smith
  • 6,105
  • 16
  • 58
  • 109
2
votes
1 answer

Python internal entity mocking

I'd like to test a method, whether it calls a specific method of a temporary internal object or not. (ConfigParser.read) So the object is created inside, and it's not accessible from the outside after the method exits. Using python 2.7 In foobar.py …
PetrosHu
  • 303
  • 2
  • 9
1 2
3
10 11