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

Is there any difference between using mock.patch.object with wraps vs side_effect?

What is the difference between the following two tests? (if any) ** in python 3.10 import unittest from unittest.mock import Mock, patch class Potato(object): def spam(self, n): return self.foo(n=n) def foo(self, n): …
Marcel Wilson
  • 3,842
  • 1
  • 26
  • 55
5
votes
1 answer

Mock a method call from an object created inside a function (python)

class Foo: def do_work: client = Client() client.widgets(self.widget_id).parts().get()`` I have the above code. Client() class is defined in another package. I am trying to unit test it with mock as follows: …
laconicdev
  • 6,360
  • 11
  • 63
  • 89
5
votes
1 answer

mock a method located in the __init__.py

I would like to mock a method which is in the init.py, but actually it is not working. There is an example to demonstrate the issue and how I tried to write the unit test: The code under test: src.main.myfile: from src.main.utils import…
user3062842
  • 53
  • 1
  • 4
5
votes
2 answers

How to throw exception from mocked instance's method?

This demo function I want to test is pretty straight forward. def is_email_deliverable(email): try: return external.verify(email) except Exception: logger.error("External failed failed") return False This function…
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
5
votes
0 answers

Mock library before import in module

I have python files as $ tree . . ├── app │   ├── __init__.py │   └── my_module.py ├── setup.py ├── test_my_module.py └── tox.ini 1 directory, 5 files my_module.py $ cat app/my_module.py from pymongo import MongoClient client =…
Nilesh
  • 20,521
  • 16
  • 92
  • 148
5
votes
0 answers

Using mock.patch's autospec option with a custom Mock subclass

I'm trying to attach a spy to a method in a class using the (backported) mock module. That is, I want to create a mock which works like the original method but offers the usual Mock features like call_count, etc. Here's the code I'm currently…
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
5
votes
2 answers

Python: How to mock class attribute initializer function

I would like to mock a module-level function used to initialize a class-level (not instance) attribute. Here's a simplified example: # a.py def fn(): return 'asdf' class C: cls_var = fn() Here's a unittest attempting to mock…
5
votes
1 answer

Mocking patched class methods is not working

I'm using Django with DRF and python mock. What I am trying to do is to test my views and mock a serializer and some methods. This is what I have: views.py from gmm_mobile.v1.serializers import RegisterParticipationSerializer from…
5
votes
0 answers

Where to mock to test a Django view that calls a service?

This is Django 1.6.8, Python 2.7, and the mock library. I have a view that calls a remote service using suds for sales tax information (this is a simplified version): def sales_tax(self, bundle_slug): bundle = get_object_or_404(Bundle,…
Melissa Avery-Weir
  • 1,357
  • 2
  • 16
  • 47
5
votes
1 answer

mock.patch() not patching class who called a couples of levels inside function call

I have a task def task(): a = worker() a.do_some_work() Worker itself is a separate class in separate module, who use Driver class like that, class Worker(object): def __init__(self): self.driver = Driver(args) ... and once…
Ph0en1x
  • 9,943
  • 8
  • 48
  • 97
5
votes
3 answers

Strict mock in python

Is there any equivalent of strict mocks in python? Some mechanism to report unintended call of mocked methods (action.step2() in this example), just like this in GoogleMock framework. class Action: def step1(self, arg): return False …
NiegodziwyBeru
  • 864
  • 1
  • 10
  • 19
5
votes
1 answer

How can I mock an external function within a method in a class

I need some help on mock. I have following code in mymodule.py: from someModule import external_function class Class1(SomeBaseClass): def method1(self, arg1, arg2): external_function(param) Now I have test code: import mock from…
user2916464
  • 491
  • 2
  • 8
  • 16
5
votes
1 answer

Django - how to change FileField upload_to path during testing

I am writing a test case for a Django model with a FileField. I'd like to change the upload path to prevent tests from having side effects on the rest of the system. I have tried passing a callable to upload_to and patching that in…
dgirardi
  • 182
  • 7
4
votes
2 answers

How to mock a Python class during testing for runtime typing checks?

I have some application method that uses the @typeguard.typechecked decorator for performing runtime checks of the passed parameters: class SalesDeal: pass @typechecked def do_something(deal: SalesDeal): pass Inside a test I have fake class…
Jens
  • 63
  • 2
4
votes
1 answer

Unit testing Mock GCS

I have a hard time to find a way to make a unit test for the read and write methods present in this class. I am trying to create a mock with the mock patch library in order to avoid calling Google Storage but I have a hard time figure out how to do…