Questions tagged [python-mock]

A test utility that patches objects with user-defined code or data.

Mock is a Python tool for unit testing. It can patch some functions or objects with mock code with some specified properties like return values. It records and provides information on how the mock object is called.

470 questions
328
votes
4 answers

Python mock multiple return values

I am using pythons mock.patch and would like to change the return value for each call. Here is the caveat: the function being patched has no inputs, so I can not change the return value based on the input. Here is my code for reference. def…
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
223
votes
4 answers

Python Mocking a function from an imported module

I want to understand how to @patch a function from an imported module. This is where I am so far. app/mocking.py: from app.my_module import get_user_name def test_method(): return get_user_name() if __name__ == "__main__": print "Starting…
nsfyn55
  • 14,875
  • 8
  • 50
  • 77
183
votes
7 answers

Assert a function/method was not called using Mock

I'm using the Mock library to test my application, but I want to assert that some function was not called. Mock docs talk about methods like mock.assert_called_with and mock.assert_called_once_with, but I didn't find anything like…
Gerard
  • 9,088
  • 8
  • 37
  • 52
175
votes
1 answer

Mocking a function to raise an Exception to test an except block

I have a function (foo) which calls another function (bar). If invoking bar() raises an HttpError, I want to handle it specially if the status code is 404, otherwise re-raise. I am trying to write some unit tests around this foo function, mocking…
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
108
votes
3 answers

Mock attributes in Python mock?

I'm having a fairly difficult time using mock in Python: def method_under_test(): r = requests.post("http://localhost/post") print r.ok # prints "" if r.ok: return StartResult() …
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
78
votes
5 answers

Python Mock object with method called multiple times

I have a class that I'm testing which has as a dependency another class (an instance of which gets passed to the CUT's init method). I want to mock out this class using the Python Mock library. What I have is something like: mockobj =…
Adam Parkin
  • 17,891
  • 17
  • 66
  • 87
74
votes
9 answers

Mocking async call in python 3.5

How do I mock async call from one native coroutine to other one using unittest.mock.patch? I currently have quite an awkward solution: class CoroutineMock(MagicMock): def __await__(self, *args, **kwargs): future = Future() …
Zozz
  • 1,875
  • 1
  • 14
  • 14
58
votes
6 answers

Better way to mock class attribute in python unit test

I have a base class that defines a class attribute and some child classes that depend on it, e.g. class Base(object): assignment = dict(a=1, b=2, c=3) I want to unittest this class with different assignments, e.g. empty dictionary, single item,…
Ivo van der Wijk
  • 16,341
  • 4
  • 43
  • 57
56
votes
1 answer

Mocking only a single method on an object

I'm familiar with other mocking libraries in other languages such as Mockito in Java, but Python's mock library confuses the life out of me. I have the following class which I would like to test. class MyClassUnderTest(object): def…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
54
votes
3 answers

Any way to reset a mocked method to its original state? - Python Mock - mock 1.0b1

I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #... def check(self): #code... value = self.A() #more code... In my first test I mock only the method…
SaiyanGirl
  • 16,376
  • 11
  • 41
  • 57
49
votes
3 answers

Mock patching from/import statement in Python

I am trying to get mock.patch to work on the following piece of sample code: from mock import patch from collections import defaultdict with patch('collections.defaultdict'): d = defaultdict() print 'd:', d This outputs the following: d:…
oneself
  • 38,641
  • 34
  • 96
  • 120
46
votes
3 answers

Django tests - patch object in all tests

I need to create some kind of MockMixin for my tests. It should include mocks for everything that calls external sources. For example, each time I save model in admin panel I call some remote URLs. It would be good, to have that mocked and use like…
tunarob
  • 2,768
  • 4
  • 31
  • 48
46
votes
3 answers

Checking call order across multiple mocks

I have three functions that I'm trying to test the call order of. Let's say that in module module.py I have the following # module.py def a(*args): # do the first thing def b(*args): # do a second thing def c(*args): # do a third…
Shaun O'Keefe
  • 1,308
  • 1
  • 11
  • 12
45
votes
1 answer

Mock Patches Appearing in the Wrong Order?

I have a test module (test.py) which imports functions from another module (keyboard.py). keyboard.py def get_keys(keyList, timeStamped): return event.getKeys(keyList=keyList, timeStamped=timeStamped) def wait_keys(keyList, timeStamped): …
Louise
  • 1,063
  • 1
  • 9
  • 20
35
votes
6 answers

Can't catch mocked exception because it doesn't inherit BaseException

I'm working on a project that involves connecting to a remote server, waiting for a response, and then performing actions based on that response. We catch a couple of different exceptions, and behave differently depending on which exception is…
Dan Oberlam
  • 2,435
  • 9
  • 36
  • 54
1
2 3
31 32