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
35
votes
2 answers

How do you mock patch a python class and get a new Mock object for each instantiation?

OK, I know this is mentioned in the manual, and probably has to do with side_effect and/or return_value, but a simple, direct example will help me immensely. I have: class ClassToPatch(): def __init__(self, *args): …
bavaza
  • 10,319
  • 10
  • 64
  • 103
34
votes
1 answer

How to mock os.walk in python with a temporary filesystem?

I'm trying to test some code that uses os.walk. I want to create a temporary, in-memory filesystem that I can populate with sample (empty) files and directories that os.walk will then return. This should save me the complexity of mocking os.walk…
jbrown
  • 7,518
  • 16
  • 69
  • 117
31
votes
4 answers

Mocking default=timezone.now for unit tests

I'm trying to write unit tests for a django app that does a lot of datetime operations. I have installed mock to monkey patch django's timezone.now for my tests. While I am able to successfully mock timezone.now when it is called normally (actually…
dgel
  • 16,352
  • 8
  • 58
  • 75
30
votes
1 answer

How do I mock part of a python constructor just for testing?

I am new to Python, so I apologize if this is a duplicate or overly simple question. I have written a coordinator class that calls two other classes that use the kafka-python library to send/read data from Kafka. I want to write a unit test for my…
jencoston
  • 1,262
  • 7
  • 19
  • 35
30
votes
3 answers

How to exclude mock package from python coverage report using nosetests

I currently try to use the mock library to write some basic nose unittests in python. After finishing some basic example I now tried to use nosetests --with-coverage and now I have the mock package and the package I tried to 'mock away' are shown in…
Frederick Roth
  • 2,748
  • 4
  • 28
  • 42
28
votes
3 answers

Customizing unittest.mock.mock_open for iteration

How should I customize unittest.mock.mock_open to handle this code? file: impexpdemo.py def import_register(register_fn): with open(register_fn) as f: return [line for line in f] My first attempt tried read_data. class…
lemi57ssss
  • 1,287
  • 4
  • 17
  • 36
27
votes
3 answers

How to mock a redis client in Python?

I just found that a bunch of unit tests are failing, due a developer hasn't mocked out the dependency to a redis client within the test. I'm trying to give a hand in this matter but have difficulties myself. The method writes to a redis…
Houman
  • 64,245
  • 87
  • 278
  • 460
26
votes
3 answers

How to mock generators with mock.patch

I have gone through the page https://docs.python.org/3/library/unittest.mock-examples.html and i see that they have listed an example on how to mock generators I have a code where i call a generator to give me a set of values that i save as a…
akshitBhatia
  • 1,131
  • 5
  • 12
  • 20
25
votes
3 answers

Python Mock Patch multiple methods in a class

Im trying to patch multiple methods in a class. Here is my simplified set up Hook.py is defined as class Hook(): def get_key(self): return "Key" def get_value(self): return "Value" HookTransfer.py defined as from Hook…
kvb
  • 625
  • 3
  • 8
  • 12
24
votes
2 answers

How to patch globally in pytest?

I use pytest quite a bit for my code. Sample code structure looks like this. The entire codebase is…
Ranjith Ramachandra
  • 10,399
  • 14
  • 59
  • 96
22
votes
2 answers

How to mock a property

I'm asking how to mock a class property in a unit test using Python 3. I've tried the following, which makes sense for me following the docs, but it doesn't work: foo.py: class Foo(): @property def bar(self): return 'foobar' def…
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
22
votes
4 answers

How do I patch an object so that all methods are mocked except one?

I have an entry point function call it main on an object that I would like to remain unmocked, since it calls several other methods on the object: class Thing(object): def main(self): self.alpha() self.bravo() def…
bbengfort
  • 5,254
  • 4
  • 44
  • 57
21
votes
3 answers

Patch __call__ of a function

I need to patch current datetime in tests. I am using this solution: def _utcnow(): return datetime.datetime.utcnow() def utcnow(): """A proxy which can be patched in tests. """ # another level of indirection, because some modules…
warvariuc
  • 57,116
  • 41
  • 173
  • 227
19
votes
3 answers

Getting an actual return value for a mocked file.read()

I'm using python-mock to mock out a file open call. I would like to be able to pass in fake data this way, so I can verify that read() is being called as well as using test data without hitting the filesystem on tests. Here's what I've got so…
Brian Hicks
  • 6,213
  • 8
  • 51
  • 77
17
votes
1 answer

python mock assert_called_with

I'm trying to understand the assert_called_with within mock but the code I wrote throws some error. import os import twitter URL = "http://test.com" def tweet(api, message): if len(message) > 40: message = message.strip("?.,.,.") …
user1050619
  • 19,822
  • 85
  • 237
  • 413
1
2
3
31 32