Questions tagged [python-unittest.mock]

mock object library (part of the Python Standard Library)

unittest.mock allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

It provides a core Mock class removing the need to create a host of stubs throughout your test suite.

330 questions
1
vote
2 answers

How to use @patch.object decorator for a method that is being called in multiple places in a class?

I'm having an implementation class where, there's this save method which is being called in multiple places within the class. So basically that method intakes an argument and returns a file url which is a string. In the class I'm trying to test, I'm…
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
1
vote
0 answers

How do I assert_called_once when using a context manager for unittest.mock?

I am relatively new to python and exploring mocking in unit tests. I have the following method to test, calc_scores so I want to mock the response to get_score_json: def calc_scores( score_id: str, ): score_json = get_score_json(score_id) …
ssloan
  • 2,226
  • 4
  • 26
  • 40
1
vote
1 answer

Unit test function removing directory contents

def clean_dir(directories): for directory in directories: for root, dirs, files in os.walk(directory, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: …
Praveen
  • 31
  • 3
1
vote
1 answer

Python Mocking - How to store function arguments to a mocked function in the mock that is returned?

Consider two explicit MagicMocks, such that one is used to create new mock instances by calling a method with arguments, and these mocks in turn are passed to the other mock's method as arguments: In [1]: from unittest.mock import MagicMock In [2]:…
davidA
  • 12,528
  • 9
  • 64
  • 96
1
vote
1 answer

How to mock connection for airflow's Livy Operator using unittest.mock

@mock.patch.dict( "os.environ", AIRFLOW_CONN_LIVY_HOOK = "http://www.google.com", clear= True ) class TestLivyOperator(unittest.TestCase): def setUp(self): super().setUp() self.dag = DAG( dag_id =…
1
vote
0 answers

Why doesn't Python mocker resolve to a immutable value?

How do you make Python's unittest.mock.patch return an object that lets you assign a callable return value? For example, I have a custom class in myclass.py defined as: class MyClass: @property def someprop(self): return 'you should…
Cerin
  • 60,957
  • 96
  • 316
  • 522
1
vote
0 answers

Patched function return value not being used with pytest

I have a test file where I'm mocking a function to return a boolean value. For example, file1.py is in a directory called service. file1.py def is_development(): return is_not_other_env() or is_not_other_other_env() APP_ENVIRONMENT =…
Paul
  • 1,101
  • 1
  • 11
  • 20
1
vote
0 answers

Django how to prevent patched method running when created in super class

I have a model with a post-save method that I don’t want to run in test: class MyModel(models.Model): def save(self, *args, **kwargs): super(MyModel, self).save(*args, **kwargs) self.post_save_method() def…
Derek Hill
  • 5,965
  • 5
  • 55
  • 74
1
vote
0 answers

How do I turn my StripeMockSubscription class into a true and proper unittest.mock MagicMock

In search of a fast stripe mock that didn't require an extra server to be running and produced realistic enough responses to use, I built the following class which works, but which is missing some functionality of the MagicMock, things like…
boatcoder
  • 17,525
  • 18
  • 114
  • 178
1
vote
1 answer

Using unittest.mock to mock a DRF response

My example is pretty basic: # urls.py from django.urls import include, path from rest_framework.routers import DefaultRouter from core import views router = DefaultRouter() router.register(r'core', views.CoreViewSet) urlpatterns = [ path('',…
1
vote
1 answer

Patching over local JSON file in unit testing

I have some Python code that loads in a local JSON file: with open("/path/to/file.json") as f: json_str = f.read() # Now do stuff with this JSON string In testing, I want to patch that JSON file to be a JSON file located in my repo's test…
1
vote
1 answer

How to mock instance attribute of django form

I'm doing a unit test where I'm mocking a Django form, but I'm having some trouble because I need to mock two things from the form: An instance attribute (token) A method (is_valid) I'm using the form in a view, importing it like this: from…
1
vote
0 answers

Mocking - Can a mock patch be toggled on/off selectively using start() stop()

I'm charged with adding a unit test to an existing TestCase object. The setup method instantiates and starts several unittest.patch objects on functions that I need to test unmocked. Is calling stop() on the relevant patch before running my tests,…
Conner M.
  • 1,954
  • 3
  • 19
  • 29
1
vote
1 answer

Mocking a request object to pass to ViewSet create() method

I'm learning unittest and unittest.mock, and struggling with the concepts and implementations primarily with mock. For context, what I'm playing with is a Django / DRF API and Redis. I'm trying to write tests which require mocking the Redis…
cjones
  • 8,384
  • 17
  • 81
  • 175
1
vote
1 answer

MagicMock's reset_mock not properly resetting sub-mock's side_effect

I have a long-lived patch on a class, whose made instance undergoes multiple batches of assertions. Please see the below code snippet for the scenario. It exposes (what I think is annoying) behavior in MagicMock.reset_mock where it seemingly…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119