Questions tagged [magicmock]

MagicMock is a subclass of Mock with default implementations of most of the magic methods.

MagicMock is a subclass of Mock with default implementations of most of the magic methods.

You can use MagicMock without having to configure the magic methods yourself.

163 questions
1
vote
1 answer

Mocking Python function according to different input arguments unittest python

I have a utility function that takes the argument case and return the value accordingly helper.py def get_sport_associated_value(dictionary, category, case): if case == 'type': return "soccer" else: return 1 #if case =…
Rahul Reddy
  • 110
  • 11
1
vote
1 answer

Mocking timedelta.total_seconds()

Is there any way to mock total_seconds() from the following code? start = datetime.now() ... end = datetime.now() diff = (end - start).total_seconds() I've tried it but getting this error TypeError: unorderable types: MagicMock() > int()
qmo
  • 3,128
  • 3
  • 17
  • 23
1
vote
1 answer

How to mock a Python class to return unique autospec Mock instance, every time the class is instantiated?

I am writing a program which creates a subprocess.Popen pipeline. I'm trying to mock subprocess.Popen such that each call returns a distinct MagicMock so I can ensure methods are called on specific (or all) processes in the pipeline. I also want…
nyanpasu64
  • 2,805
  • 2
  • 23
  • 31
1
vote
1 answer

How can I do http.server do_GET mock unit test?

I am beginner in Unit test. I would like to know how can I do mock unit test of following functions. import ... class A(BaseHTTPRequestHandler): def do_GET(self): client_ip = self.client_address[0] if client_id == '10.10.10.10': …
Iqbal
  • 235
  • 8
  • 20
1
vote
1 answer

python mocking check if a method of an object was accessed(not called)

class A(): def tmp(self): print("hi") def b(a): a.tmp # note that a.tmp() is not being called. In the project I am working on, a.tmp is being passed as a lambda to a spark executor. And as a.tmp is being invoked in an executor(which…
claudius
  • 1,112
  • 1
  • 10
  • 23
1
vote
1 answer

Mock nested import in Python with MagicMock

My file (ensure_path.py): import os def ensure_path(path): if not os.path.exists(path): os.makedirs(path) return path My test: import unittest from unittest.mock import patch, MagicMock from src.util.fs.ensure_path import…
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
1
vote
1 answer

Python magicmock causes 'NoneType' object has no attribute 'unregister'

I'm trying to patch a function call with a custom mock I wrote subscriberMock = MagicMock(side_effect=subscriber) subscriberMock.return_value.unregister.return_value = True with patch('rospy.Subscriber', subscriberMock): …
cjds
  • 8,268
  • 10
  • 49
  • 84
1
vote
1 answer

How patch a method being called from an instance in python

I am trying to write a unittest for my file abc.py. I am not able to set a return value for a method being called from a class instance. Here is my file: abc.py class ABC(object): def __init__(self): pass def fetch_id(self): …
Bilal Baqar
  • 208
  • 2
  • 12
1
vote
1 answer

How to mock a python library using patch in python regardless where it is used?

In the following question the function that uses the ftplib is defined in the same file, which makes it trivial to patch ('ftplib.FTP') Mocking ftplib.FTP for unit testing Python code My question is: How should I proceed if, in my test, I would like…
1
vote
2 answers

Magic Mock fails when nosetest discovers tests

I'm using MagicMock to test a function in a web app. The function is imported directly from a module. The expected behaviour is: when the function being tested is called, it calls on a third party api (but I'm mocking this for my test). That returns…
JasTonAChair
  • 1,948
  • 1
  • 19
  • 31
1
vote
0 answers

cannot mock import statement using unittest.patch

I have an import statement inside a function and I want to mock that import statement using patch, but couldn't. class A(): """mock object""" @staticmethod def run(): print('ran') @patch('something') def…
polymath99
  • 11
  • 2
1
vote
1 answer

mocking python class dictionary

I am fairly new to python. I am trying to use mock to write a unit test. Here is the pattern of the code. # mod3.py import mod1.class1 import mod2.class2 d = { "c1": class1 "c2": class2 } def func1(c, v): cl = d[c] o = cl().meth1(v) …
barmd
  • 11
  • 4
1
vote
1 answer

MagicMock not called inside django signals

I have some model class Foo(models.Model): name = models.CharField(...) url = models.URLField(...) foo_pre_save_(sender, instance, *args, **kwargs): r = urlopen(instance.url) # Magic mock is not called html =…
Ben
  • 6,986
  • 6
  • 44
  • 71
1
vote
1 answer

Why does the behavior of the patch library change depending on how values are imported?

The patch function from the mock library is sensitive to how things are imported. Is there a deep reason why I can't just use the fully qualified name where the function was originally defined regardless of how it is imported in other modules? using…
Greg Nisbet
  • 6,710
  • 3
  • 25
  • 65
1
vote
0 answers

Python patch not working, no calls being recorded

First time working with MagicMock, and I think I've confused myself. Testing a django project, so in a file called services.py I have these important elements (extremely simplified and with many bits cut out, of course):: from django.template.loader…
fildred13
  • 2,280
  • 8
  • 28
  • 52