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

Python how to reuse a Mock to avoid writing mock.patch multiple times?

Given code like the following: import flask import time app = flask.Flask(__name__) def authorize(): print('starting authorize io') time.sleep(1) print('done authorize io') class BlockingIo(): def __init__(self, n): self.n…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
11
votes
2 answers

How to mock in python and still allow the actual code of mocked function to execute

I have recently started using the mock framework in python. It seems that if I patch a function, the actual code is not called - which means that the database changes etc that this actual function does is not implemented. I have been trying to go…
dowjones123
  • 3,695
  • 5
  • 40
  • 83
11
votes
2 answers

How to patch classmethod with autospec in unmocked class?

I want to assert that one classmethod in a Python class calls another classmethod with a certain set of arguments. I would like the mocked classmethod to be "spec-ed", so it detects if it is called with the wrong number of arguments. When I patch…
scanny
  • 26,423
  • 5
  • 54
  • 80
10
votes
2 answers

Python testing: using a fake file with mock & io.StringIO

I'm trying to test some code that operates on a file, and I can't seem to get my head around how to replace using a real file with mock and io.StringIO My code is pretty much the following: class CheckConfig(object): def __init__(self, config): …
bordeltabernacle
  • 1,603
  • 5
  • 24
  • 46
10
votes
2 answers

Mock not working on module function

I have written the function send_formatted_email which formats email subject and message then calls the send_email function in a separate module. Now I have to test that send_formatted_email is calling send_email with the expected arguments. For…
Afnan Nazir
  • 458
  • 2
  • 4
  • 13
10
votes
4 answers

How to test Retry in Celery application in Python?

I'm trying to test if the application is retrying. @celery.task(bind=False, default_retry_delay=30) def convert_video(gif_url, webhook): // doing something VideoManager().convert(gif_url) return except Exception as exc: …
toy
  • 11,711
  • 24
  • 93
  • 176
10
votes
3 answers

Using python's mock to temporarily delete an object from a dict

I am writing a test for some code that checks for a value in os.environ (I know this isn't optimal, but I have to go with it). I would like to remove an entry from os.environ for the duration of the test. I am not sure if mock supports this. I…
Wisco crew
  • 1,337
  • 1
  • 17
  • 25
10
votes
2 answers

How should I test using Mocks in Python?

I can see two different approaches to injecting mocks into python code that I want to test: Dependency Injection: Allow the collaborating classes to passed into the constructor of the object under test, and pass in mock objects (and factories where…
lexicalscope
  • 7,158
  • 6
  • 37
  • 57
9
votes
2 answers

Python - why mock patch decorator does not pass the mocked object to the test function when `new` argument is not DEFAULT

in Python 3.6, I use unittest.mock.patch to patch a function like this: class SampleTest(TestCase): @mock.patch('some_module.f') def test_f(self, mocked_f): f() mocked_f.assert_called() This passes a mock.MagicMock() as…
Reza
  • 1,065
  • 1
  • 10
  • 18
9
votes
2 answers

Patch method only in one module

For example, I have some module(foo.py) with next code: import requests def get_ip(): return requests.get('http://jsonip.com/').content And module bar.py with similiar code: import requests def get_fb(): return…
9
votes
0 answers

How to get a diff from python's `mock.assert_called_with()`?

When calling unittest.TestCase.assertEqual() on two complex dictionaries, I get a nice diff. Is there any way to get a diff from Python 2.7's mock.Mock.assert_called_with? I'm testing calls that take some dict parameters with complex values, and…
Eli Rose
  • 6,788
  • 8
  • 35
  • 55
9
votes
2 answers

Changing the second result of a function call with mock

I have a loop that looks like this: for i in range(len(some_list)): response = requests.post(some_url, some_params) if response.status_code != HTTPOk: # do something What I would like to do is change response of requests.post in the…
TJ1S
  • 528
  • 3
  • 12
9
votes
1 answer

Mocking Python iterables for use with Sphinx

I'm using Sphinx to document a project that depends on wxPython, using the autodocs extension so that it will automatically generate pages from our docstrings. The autodocs extension automatically operates on every module you import, which is fine…
Chris Krycho
  • 3,125
  • 1
  • 23
  • 35
8
votes
1 answer

python mocking sqlalchemy connection

I have a simple function that connects to a DB and fetches some data. db.py from sqlalchemy import create_engine from sqlalchemy.pool import NullPool def _create_engine(app): impac_engine = create_engine( app['DB'], …
user1050619
  • 19,822
  • 85
  • 237
  • 413
8
votes
2 answers

How to mock objects methods return value

what I currently have is: def some_method(): some_obj = some_other_method() # This is what I want to mock return value of: some_obj.some_obj_some_method() @patch('some_package.some_other_method') def…
CodeNewbee
  • 176
  • 1
  • 2
  • 9