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

How to represent unittest.mock.call().__str__()

One of the mocks that I'm using contains the call call().__str__(). Output of my_mock.mock_calls [call(, indent=2, sort_keys=True), call().__str__(), # <-- what I'm trying to represent …
aydow
  • 3,673
  • 2
  • 23
  • 40
4
votes
1 answer

How does one mock.patch a function inside a Flask Blueprint?

I'm trying to use the unittest.mock library to patch a function inside a Flask Blueprint. Specifically, I'm trying to patch the os module inside the blueprint. Simplified code (cannot provide all/actual code): test_route.py (the test I am trying to…
cischa
  • 414
  • 2
  • 8
4
votes
2 answers

Python Mocking - How to mock Google's storage.client?

Could anybody help with GCP API mocking? Here's the function func.py: import re from google.cloud import storage def is_all_log_entries_sink(sink): storage_client = storage.Client() if 'filter' not in sink and 'storage.googleapis.com' in…
4
votes
4 answers

Mocking a RelatedManager in Django 2

This question is directly related to this question, but that one is now outdated it seems. I am trying to test a view without having to access the database. To do that I need to Mock a RelatedManager on the user. I am using pytest and…
pymarco
  • 7,807
  • 4
  • 29
  • 40
4
votes
2 answers

How to mock modules python, patch does not find the attribute

In a function I'm using uuid1 that I want to patch. def myFunction(): my_value = uuid4.int smth else.. I want to be able to mock my_value so it always returns the same number in my unit test, because I need it for further use. I tried…
user10658941
4
votes
1 answer

How to redefine MagicMock __str__ method?

I'm trying to autogenerate documentation for Readthedocs. I mock some dependencies as they suggest in FAQ, but from type annotations of functions I get some parts of documentation to look like Return type: Which…
Bunyk
  • 7,635
  • 8
  • 47
  • 79
4
votes
1 answer

How to test if instance's method is called with "self" when mocking?

I'm using unittest.mock, this wonderful library. However I was surprised by an unexpected behavior, and I don't see an obvious solution. I'm using it for my unit tests and it is vital to understand perfectly how it behaves to have useful tests. I…
Doomsday
  • 2,650
  • 25
  • 33
4
votes
1 answer

How can I fake a missing dependency for unit testing in python?

I have written a python module that depends on a third-party package that isn't available on my CI testing machine, so I can't test my module remotely because I can't get past the import dependency statement. If we assume there's no way I can just…
Duncan Macleod
  • 1,045
  • 11
  • 21
4
votes
2 answers

How can I mock/patch an associative array in python

I have a module with a dictionary as associative array to implement a kind-of switch statement. def my_method1(): return "method 1" def my_method2(): return "method 2" map_func = { '0': my_method1, '1': my_method2 } def…
epc
  • 194
  • 4
  • 12
4
votes
3 answers

Mocking function argument names to work with inspect.getargspec

I'm trying to mock a function for a test that eventually passes the mock through the inspect.getargspec function. I'd like to use some specific names as argument names of the mocked function. As far as I know, there's no specific way to mock…
liori
  • 40,917
  • 13
  • 78
  • 105
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
4
votes
2 answers

Mocking out a call within a celery task

I have a flask app that runs a celery task. I'm trying to mock out a single API call that happens deep within that task. views.py from mypackage.task_module import my_task @app.route('/run_task') def run_task(): task = my_task.delay() return…
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
4
votes
1 answer

Adding mock object into sqlalchemy collection raises exception

I am writing a unit test for SQLAlchemy model class with one-to-many relationship but am not able to add a mocked object into the collection. Classes under test: class PCLRun(Base): __tablename__ = 'pcl_runs' id = Column(Integer,…
4
votes
1 answer

python mocking third party modules

im trying to test some classes that process tweets. Im using sixohsix twitter to deal with Twitter API. I have a class that acts as a facade for the Twitter classes, and my idea was to mock the actual sixohsix classes to simulate the arrival of…
Sebastian
  • 1,243
  • 1
  • 18
  • 34
4
votes
1 answer

Assert that *any* attribute was set

I have a Python mock object and I would like to assert whether any attribute of that object was set. I do not believe the PropertyMock will work for my purposes, because I must know if any attribute was set, not a particular property. It also does…
Nathan Buesgens
  • 1,415
  • 1
  • 15
  • 29