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

Python: Unpack call_args_list from unittest.mock

i would like to unpack the arguments of a mocked method. I have a mocked subscriber which is called by the code under test and i would like to verify the invocation of the notify() method. class Subscriber: def notify(self, event): …
kKdH
  • 512
  • 1
  • 8
  • 17
0
votes
0 answers

Can I mock `open` (and other builtins) when testing my functions?

When writing unit tests, I can use unittest.mock to isolate dependencies and monitor use of the mocked functions. Can I do the same with calls to builtin functions? In particular, it would be nice to mock open() so that I can provide test input, or…
alexis
  • 48,685
  • 16
  • 101
  • 161
0
votes
0 answers

Python Unittest Mock - Reroute a function's write() to a temp dir?

I'm currently trying to write a unittest which checks whether or not a function has written an error log. For testing purposes, the error log is to be written to a temporary directory but I am unsure how I can re-route the location of a write() call…
Bryan Stafford
  • 301
  • 2
  • 12
0
votes
1 answer

Subclassing Mock in Python 2.7

I have a set of unit tests which will repeatedly be using a certain cooperator class Rental that I want to mock, with the same arguments passed every time. To make this easier, I want to make a subclass of mock.Mock and pass the arguments on…
0
votes
2 answers

Autospec not failing when call is made with too few args

I am trying figure out the best way to use autospec in my tests. In the following scenario I use autospec to detect when a call is made with too many args. This scenario works as expected when called with $ python filename.py. import unittest import…
Dan
  • 1,874
  • 1
  • 16
  • 21
0
votes
0 answers

Patch global variable once in several modules

Imagine that we have a module one.py with some global variable glob_var=object(). Also I have some other modules on different levels that import glob_var using relative import, such as: from .one.py import glob_var from ..one.py import glob_var…
0
votes
0 answers

TypeError: Python unit testing using mock

I am trying to test a function using python mock. Foo.py session = scoped_session(sessionmaker(bind=db_state.engine)) @use_kwargs({ 'user_id': fields.Integer(location='json', required=True) }) def create_user(user_id): with session as s: …
user3527975
  • 1,683
  • 8
  • 25
  • 43
0
votes
0 answers

Is this possible to check if mock being called with one of parameters list

Let's say we have some method: def sleep(self, seconds=0): seconds = seconds or self.wait_timer time.sleep(seconds) And method-caller: def _sleep_before_next(self, number: int) -> None: for counter, wait_timers in…
valignatev
  • 6,020
  • 8
  • 37
  • 61
-1
votes
2 answers

Understanding where to patch

In the docs, it explains why patching at at the function definition level doesn't work: Imagine we have a project that we want to test with the following structure: a.py -> Defines SomeClass b.py -> from a import SomeClass ->…
user1187968
  • 7,154
  • 16
  • 81
  • 152
-1
votes
1 answer

Which method is the best way to mock classes used in the `__init__` of a class being tested?

I was hoping that someone could give me guidance on some of the approaches I've tried for mocking. I'm really trying to understand what the best method is for this general case (I think general enough). And whether or not there are better approaches…
-1
votes
1 answer

How to patch a class in the right place

I am having trouble to patch a class that is imported and set in a dictionary. Given these files: lib/ foo/ conf.py foo.py # conf.py from .foo import Foo CONF = {"class": Foo} # foo.py class Foo(): pass Why Foo is not MagicMock in…
-1
votes
3 answers

Override 'is' operator for mocked object

Simplified code: I have a function that expects either a number or None, and returns True if it's None, and False if it's a number, like: def function(var): return var is None I want to pass a mocked object and test it with the is operator like…
-1
votes
1 answer

Mock global function call while importing

Suppose I have a file called a.py with code like import mod1 mod1.a() def b(): print("hi") Now if I want to mock fun b() then unittest.py while have import statement at top like from a import b at time of import mod1.a() will be called. How can…
shivams
  • 2,597
  • 6
  • 25
  • 47
-2
votes
1 answer

Python unittest: replace (or patch) a function via mock

I read a lot of about unittest.mock but I am unable to transfer this to my own scenario. So I created a minimal working example here. I simply replace a function before a test and set it back to the original one at the tests end - without using…
buhtz
  • 10,774
  • 18
  • 76
  • 149
1 2 3
21
22