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

MagicMock and wrap

Why doesn't the "wraps" keyword work consistently for MagicMock objects? Normal methods are passed through to the wrapped object, but not "special" methods. In the test below, the first assertion passes, the second fails. import mock import…
dgorur
  • 1,638
  • 4
  • 16
  • 26
5
votes
1 answer

How to assert_called_with the first call if the method has been called twice in another method?

eg in t.py def a(obj): print obj def b(): a(1) a(2) then: from t import b with patch('t.a') as m: b() m.assert_called_with(1) I get: AssertionError: Expected call: a(1) Actual call: a(2)
James Lin
  • 25,028
  • 36
  • 133
  • 233
4
votes
1 answer

How to mock using sys.modules and with mock.patch (Python interference on static functions)

So I've this code that mocks two times, the first time by mocking imports with: sys.modules['random'] = MagicMock() The second time happens inside the unittest of a function that used that import, for example a function that used random The tests.…
4
votes
0 answers

pytest stubbing a method for all tests

Here is my class under test: class UnderTest(): def __init__(self, url, token): self.paths = self._auth(url, token) def _auth(self, url, token): creds = BasicAuthentication(token=token) connection = Connection(url,…
a11smiles
  • 1,190
  • 1
  • 9
  • 21
4
votes
2 answers

Unittest.mock - how to mock a call to cursor.execute() from connection object?

I am trying to stub out cursor.execute() in the following code with mock such that I can test execute is called with a correctly formatted query: // Module ABC def buildAndExecuteQuery( tablesAndColumnsToSelect ): '''Build and execute a query. …
bwrabbit
  • 529
  • 1
  • 4
  • 25
4
votes
2 answers

Python mock() not mocking the return value

I'm having some trouble with Python mock() and I'm not familiar enough to figure out what's going on with it. I have an abstract async task class that looks something like: class AsyncTask(object): @classmethod def enqueue(cls): …
user358829
  • 741
  • 1
  • 7
  • 17
3
votes
2 answers

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() object in this case such that it doesn't have the…
3
votes
0 answers

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 successful and unsuccessful lists class A(): def…
3
votes
1 answer

Spying on a pure function call in my unit test

I am trying to mock the call to a function and still have the effect of the function apply. I found the solution using Python wraps, but all examples I found are applied to mocking the member method of a class. In my case, I have a pure function…
3
votes
1 answer

is there a simple way of adding __name__ to MagicMock attributes recursively

Statement when I ask a __name__ of a MagicMock object I get an AttributeError => which is just how MagicMock works I think. from mock import MagicMock a =…
studioj
  • 1,300
  • 17
  • 32
3
votes
1 answer

Python HTTP Post method returns response as magicmock object instead of value

I am trying to check the response status code after trigerring some API with a POST method, Response status code is of Magicmock instance type, i am checking whether the status code is inbetween 400 and 500 using comparison operator which works in…
3
votes
0 answers

pytest mocking with boto3

just learning python mocking in general and struggling with using Magicmock and pytest with boto3. Here is my code block def upload_to_s3(self, local_file, bucket, dest_file): self.local_file = local_file self.bucket = bucket …
tdw_8
  • 31
  • 2
3
votes
2 answers

Python MagicMock assert_called_once_with not taking into account arguments?

I am setting up a MagicMock instance, calling the same method twice with different arguments, and setting my assertion to only validate for one set of arguments. Python: 3.5.2 from unittest.mock import MagicMock my_mock =…
djm
  • 119
  • 1
  • 5
3
votes
2 answers

How does MagicMock avoid throwing AttributeError when a random method is called?

In Python, if you call a method that doesn't exist it throws an AttributeError. Ex >>> class A: ... def yo(self): ... print(1) ... >>> a = A() >>> a.yo() 1 >>> a.hello() Traceback (most recent call last): File "", line 1,…
2ank3th
  • 2,948
  • 2
  • 21
  • 35
3
votes
1 answer

Patch a method outside python class

I am interested in patching a method which is called by another method in one file. Example - original.py file contains - def A(): a = 10 b = 5 return a*b; def B(): c = A() return c* 10 I want to write unit test for this file , say…
psbits
  • 1,787
  • 5
  • 19
  • 34
1
2
3
10 11