python-mock Questions

1

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(...
Waechter asked 22/7, 2022 at 21:35

5

Solved

I want to understand how to @patch a function from an imported module. This is where I am so far. app/mocking.py: from app.my_module import get_user_name def test_method(): return get_user_nam...
Deglutinate asked 21/4, 2013 at 17:48

3

Solved

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 i...
Rayner asked 28/4, 2015 at 16:7

7

Solved

I'm working on a project that involves connecting to a remote server, waiting for a response, and then performing actions based on that response. We catch a couple of different exceptions, and beha...
Derzon asked 29/7, 2015 at 23:49

2

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....
Squeak asked 13/8, 2020 at 18:38

1

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 t...
Straub asked 7/9, 2016 at 15:37

3

Solved

I'm asking how to mock a class property in a unit test using Python 3. I've tried the following, which makes sense for me following the docs, but it doesn't work: foo.py: class Foo(): @property ...
Pronto asked 9/3, 2017 at 20:8

3

Solved

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_metho...
Seth asked 26/6, 2019 at 12:1

4

Solved

I am using pythons mock.patch and would like to change the return value for each call. Here is the caveat: the function being patched has no inputs, so I can not change the return value based on th...
Elwoodelwyn asked 22/7, 2014 at 20:25

3

Trying to write a testcase for my class based function. This is skeleton of my class class Library(object): def get_file(self): pass def query_fun(self): pass def get_response(self): self....
Beneath asked 21/11, 2017 at 7:43

6

Solved

I have a base class that defines a class attribute and some child classes that depend on it, e.g. class Base(object): assignment = dict(a=1, b=2, c=3) I want to unittest this class with differe...
Victorious asked 11/3, 2014 at 11:39

5

Solved

I have a class that I'm testing which has as a dependency another class (an instance of which gets passed to the CUT's init method). I want to mock out this class using the Python Mock library. Wh...
Gambol asked 5/10, 2011 at 18:9

4

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) retur...
Saavedra asked 10/6, 2015 at 0:21

3

Solved

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 th...
Dielle asked 24/3, 2015 at 21:30

1

I have the following (simplified) FBV: def check_existing_contacts(request): if request.is_ajax and request.method == "GET": print('Function called') return mailgun_validate_email(requ...

7

Solved

I'm using the Mock library to test my application, but I want to assert that some function was not called. Mock docs talk about methods like mock.assert_called_with and mock.assert_called_once_with...
Elegiac asked 29/8, 2012 at 21:59

2

Solved

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&qu...
Aeronautics asked 4/2, 2018 at 8:48

3

Solved

I have an application that imports a module from PyPI. I want to write unittests for that application's source code, but I do not want to use the module from PyPI in those tests. I want to mock it ...
Hyalo asked 19/12, 2016 at 10:43

2

Solved

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(...
Amphimacer asked 31/7, 2019 at 23:20

3

Solved

What I would like: Ensure that all instances of Foo that are created inside the with statement have their foo instance method wrapped in a MagicMock via wraps=Foo.foo. The reason I want this is so ...
Waffle asked 26/6, 2017 at 20:51

9

Solved

How do I mock async call from one native coroutine to other one using unittest.mock.patch? I currently have quite an awkward solution: class CoroutineMock(MagicMock): def __await__(self, *args, ...
Acis asked 9/9, 2015 at 12:54

3

Solved

I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #... def check(self): #code... value = self.A() #more code... In my first test I mock onl...
Dermatoid asked 31/7, 2012 at 18:13

3

Solved

I just found that a bunch of unit tests are failing, due a developer hasn't mocked out the dependency to a redis client within the test. I'm trying to give a hand in this matter but have difficulti...
Nganngc asked 29/5, 2015 at 13:47

4

Solved

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 RelatedMana...
Provisional asked 29/4, 2019 at 16:41

4

I have an entry point function call it main on an object that I would like to remain unmocked, since it calls several other methods on the object: class Thing(object): def main(self): self.alph...
Cornu asked 3/3, 2016 at 18:44

© 2022 - 2025 — McMap. All rights reserved.