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
6
votes
1 answer

Python: mock patch class once for all test methods?

Consider this example: module.py: class LST: x = [1] class T: def __init__(self, times): self.times = times def t1(self): return LST.x * self.times def t2(self): return LST.x * (self.times+1) def…
Andrius
  • 19,658
  • 37
  • 143
  • 243
6
votes
2 answers

Pass parameter to side_effect function for patching in unittest.mock

I'm using patch from unittest.mock to change the behavior of a remote API call in my test. I have three different functions that return three different json files that represent the mock data to be returned from the API. For each mock api call, I am…
briancaffey
  • 2,339
  • 6
  • 34
  • 62
6
votes
1 answer

What is the reason that mock.patch ignores a fully imported function?

Today I realized that it matters for unittest.mock.patch how I import a function. Depending on the used way a mock.patch call worked or was ignored. In Python we typically import a function with: an import statement like import os or a from ...…
Jon
  • 11,356
  • 5
  • 40
  • 74
6
votes
2 answers

Mock method which returns same value passed as argument

How do I mock python method using python unittest.mock which will return same value passed as argument, I tried, from unittest.mock import MagicMock def dummy_function(value): "Will return same value as value passed to the function" return…
Murtuza Z
  • 5,639
  • 1
  • 28
  • 52
6
votes
1 answer

How to use abc abstract base class as mock spec?

I have an abstract base class: import abc import six @six.add_metaclass(abc.ABCMeta) class A(object): @abc.abstractmethod def f(self, arg1): pass I'd like to use this class as a spec for a mock. import mock mock_a =…
Cirdec
  • 24,019
  • 2
  • 50
  • 100
6
votes
2 answers

How can I use mock_open with a Python UnitTest decorator?

I have a test as follows: import mock # other test code, test suite class declaration here @mock.patch("other_file.another_method") @mock.patch("other_file.open", new=mock.mock_open(read=["First line", "Second line"]) def…
YPCrumble
  • 26,610
  • 23
  • 107
  • 172
6
votes
1 answer

Mocking a module imported inside of a function

Is it possible to mock a module that's imported inside of a function? for instance def my_func(input): import something_else something_else.do_something(input) I have an import inside of the function because of a circular dependency. Based…
aamiri
  • 2,420
  • 4
  • 38
  • 58
6
votes
2 answers

How to call mocked method in Python mock

I want to create a mock method that calls the underlying method being mocked. I'm imagining something like the following, but I can't find any documentation about the mock object holding a reference to the object being mocked, which I've denoted as…
cbare
  • 12,060
  • 8
  • 56
  • 63
6
votes
1 answer

Python mock patch instance method and check call arguments

I am using Mock (http://mock.readthedocs.org/en/latest/) library with Python 2.7. I have a main function that calls a few other functions that I am trying to test. The other functions that it calls are other instance methods (so for example, def…
darksky
  • 20,411
  • 61
  • 165
  • 254
6
votes
1 answer

Not able to mock urllib2.urlopen using Python's mock.patch

Below is a code snippet of my api.py module # -*- coding: utf-8 -*- from urllib2 import urlopen from urllib2 import Request class API: def call_api(self, url, post_data=None, header=None): is_post_request = True if (post_data and…
Hussain
  • 5,057
  • 6
  • 45
  • 71
6
votes
1 answer

Python mock function with only specific argument

I'm new to Python and I'm trying to mock a function only when a specific argument is passed. If other than the desired argument is passed, I'd like to call the original function instead. In Python 2.7 I tried something like this: from foo import…
jpemberthy
  • 7,473
  • 8
  • 44
  • 52
6
votes
2 answers

Does python's `unittest.mock.patch` mutate global state?

I'm trying to determine if Python's mock.patch (unittest.mock.patch in Py3) context manager mutates global state, i.e., if it is thread-safe. For instance: let's imagine one thread patches function bar within function foo with a context manager, and…
Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73
6
votes
1 answer

An easy way to mock loosely defined Python dict objects

Is there an easy way to mock loosely defined dict objects in Python? For example, how can I easily express that given a dict input, I want to check whether or not each value in it conforms to a particular meta-definition, like minimum and maximum…
Martin Thorsen Ranang
  • 2,394
  • 1
  • 28
  • 43
6
votes
5 answers

Python mock library: Is there any way to get corresponding return values from magic mock calls?

When writing Python tests with the mock library, I often get "what arguments a method is called with" like this, from __future__ import print_function import mock m = mock.MagicMock(side_effect=lambda x: x * x) m(4) print("m called with: ",…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
6
votes
1 answer

Mocking functions in Django's class based views

I'm using Django REST Framework for an API that I'm working on. For a couple of reasons, I would like to use class-based views. However, I'm a bit particular about my unit testing, and I never allow my unit tests to touch the database. Note: I…
David S
  • 12,967
  • 12
  • 55
  • 93