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
8
votes
0 answers

Mocking grpc response message in python

Recently I have been getting my hand dirty to mock gRPC calls in python using MagicMock() but I have been not successful in mocking gRPC response message which I have got. Here is the piece of code python client code def senor_read(self,…
pkumarn
  • 1,383
  • 4
  • 22
  • 29
8
votes
2 answers

What is the difference between using the '@patch.object' and 'with patch.object' in Python?

In writing unit tests for my application, I have always been using the @mock.patch and @patch.object decorators. But now, for some unit tests when I use the decorator, I receive an error 'TypeError: staticmethod object is not an iterator'. But with…
8
votes
1 answer

Avoid executing __init__ of mocked class

I have a class with an expensive __init__ function. I don't want this function called from tests. For the purpose of this example, I made a class that raises an Exception in __init__: class ClassWithComplexInit(object): def __init__(self): …
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
8
votes
1 answer

Mocking a Celery task call in Python with patch

Patching a Celery task call with a mocked return value returns instead of the expected return_value defined by mock_task.get.return_value = "value". However, the mocked task functions correctly within my unit…
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
8
votes
1 answer

Python Mock - How to get the return of a MagicMock as if it was a normal method

For example: import mock class MyClass(object): def foo(self, x, y, z): return (x, y, z) class TestMyClass(TestCase) @mock.patch('MyClass') def TestMyClass(self, MyClassMock): foo_mock = MyClassMock.foo() …
Nice Guy
  • 479
  • 2
  • 6
  • 12
8
votes
2 answers

How to use Mock library to mock a Django ForeignKey value?

I have a model and I'm trying to test validation without invoking the database layer. Rather than describe with words I'll just post up some example code. The issue here is the ForeignKey relationship to Bar, which isn't relevant to what I'm trying…
Dan Passaro
  • 4,211
  • 2
  • 29
  • 33
8
votes
2 answers

Can I add an instance method to a Python "Mock" object?

I would like to create a mock.Mock() object, then add a method called session that acts like an instance method, which is passed a self reference to the mock object, allowing the method to add state to the mock object. Is this possible (without…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
7
votes
2 answers

Can a python constructor be mocked without mocking other properties of the object?

Is it possible to mock a python constructor while continuing to use the production version other fields/functions on the same name? For example, given the production code: class MyClass: class SubClass: def __init__(self) -> None: …
tonicsoft
  • 1,736
  • 9
  • 22
7
votes
2 answers

Mock property return value gets overridden when instantiating mock object

Background I'm trying to set up a test fixture for an application I'm writing in which one of my classes is replaced with a mock. I'm happy to leave most of the attributes of the mock class as the default MagicMock instances (where I'm only…
Tagc
  • 8,736
  • 7
  • 61
  • 114
7
votes
2 answers

Python Requests Mock doesn't catch Timeout exception

I wrote a unittest to test timeout with the requests package my_module.py: import requests class MyException(Exception): pass def my_method(): try: r = requests.get(...) except requests.exceptions.Timeout: raise…
Hank
  • 3,367
  • 10
  • 48
  • 86
7
votes
1 answer

Using python mocking library on sqlalchemy

I'm using sqlalchemy to query my databases for a project. Side-by-side, I'm new to unit testing and I'm trying to learn how can I make unit tests to test my database. I tried to use mocking library to test but so far, it seems to be very…
user2430771
  • 1,326
  • 4
  • 17
  • 33
7
votes
1 answer

How to mock Django Model Queries

I would like to mock the following CanonPerson model def compute(self, is_send_emails, test_email_address): cpses = CanonPerson.objects.filter(persons__vpd=6, persons__country="United States", …
Houman
  • 64,245
  • 87
  • 278
  • 460
7
votes
4 answers

Patching a function with Mock only for one module?

I need to patch os.listdir and other os functions to test my Python function. But when they're patched, the import statement fails. Is it possible to patch this function only inside that single module, the real one, and leave tests.py work…
culebrón
  • 34,265
  • 20
  • 72
  • 110
6
votes
1 answer

Does patch.multiple work with pytest as a decorator

I have a test_tmp.py adopted from https://docs.python.org/3/library/unittest.mock.html#patch-multiple from unittest.mock import DEFAULT, MagicMock, patch thing = object() other = object() @patch.multiple('__main__', thing=DEFAULT,…
zyxue
  • 7,904
  • 5
  • 48
  • 74
6
votes
1 answer

Mocking a method's return value does not work

While testing the create_response method, I cannot seem to mock the return value of the get_external_response method. /foo/response from abc import ABCMeta, abstractmethod def create_response(url, type): query = create_query(url, type) …
timmy78h
  • 183
  • 1
  • 3
  • 10