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

How to create a subscriptable Mock object?

Suppose, I have a code snippet as foo = SomeClass() bar = foo[1:999].execute() To test this, I have tried something as foo_mock = Mock() foo_mock[1:999].execute() Unfortunately, this raised an exception, TypeError: 'Mock' object is not…
JPG
  • 82,442
  • 19
  • 127
  • 206
16
votes
3 answers

How to mock just the method inside the class

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.get_file() …
user3895077
16
votes
3 answers

Mock an entire module in python

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 entirely (the testing machine will not contain that…
TzurEl
  • 792
  • 3
  • 9
  • 15
16
votes
1 answer

PyCharm complains about patch.object but why?

Pretty basic setup: import mock from mock import patch def test_foo(): with patch.object(MyObject...): # do some stuff here... PyCharm warning: Cannot find reference 'object' in 'function'. If to pop up mock.py, patch.object is defined…
Schultz9999
  • 8,717
  • 8
  • 48
  • 87
15
votes
1 answer

Pytest raise test failing for requests.raise_for_status()

I've recently started using pytest, and even more recently started using mock for mocking the requests library. I have made a requests.Response object okay, and for a 200 status code it works fine. What I'm trying to do here, is to use…
huwf
  • 153
  • 1
  • 1
  • 5
15
votes
2 answers

Django ORM - mock values().filter() chain

I am trying to mock a chained call on the Djangos model.Manager() class. For now I want to mock the values() and filter() method. To test that I created a little test project: Create a virtual environment Run pip install django mock mock-django…
Jens
  • 20,533
  • 11
  • 60
  • 86
14
votes
3 answers

Mock Stripe Methods in Python for testing

So I am trying to mock all the stripe web hooks in the method so that I can write the Unit test for it. I am using the mock library for mocking the stripe methods. Here is the method I am trying to mock: class AddCardView(APIView): """ * Add card…
Shubham
  • 3,071
  • 3
  • 29
  • 46
14
votes
3 answers

Mocking ftplib.FTP for unit testing Python code

I don't know why I'm just not getting this, but I want to use mock in Python to test that my functions are calling functions in ftplib.FTP correctly. I've simplified everything down and still am not wrapping my head around how it works. Here is a…
Tamerz
  • 897
  • 1
  • 10
  • 25
13
votes
1 answer

Mocking file objects or iterables in python

Which way is proper for mocking and testing code that iters object returned by open(), using mock library? whitelist_data.py: WHITELIST_FILE = "testdata.txt" format_str = lambda s: s.rstrip().lstrip('www.') whitelist = None with…
Victor Miroshnikov
  • 594
  • 1
  • 4
  • 13
13
votes
1 answer

How to pass the arguments to the new_callable from mock.patch?

I've got the code and the test files: code.py class Code: def do_something_inside(self, a, b, c): return a-b-c def do_something(self, b, c): self.do_something_inside(30, b, c) test.py import unittest import unittest.mock…
13
votes
2 answers

Inheriting a patched class

I have a base class extending unittest.TestCase, and I want to patch that base class, such that classes extending this base class will have the patches applied as well. Code Example: @patch("some.core.function", mocked_method) class…
sihrc
  • 2,728
  • 2
  • 22
  • 43
12
votes
1 answer

How do I correctly use mock call_args with Python's unittest.mock?

Consider the following files: holy_hand_grenade.py def count(one, two, five='three'): print('boom') test_holy_hand_grenade.py from unittest import mock import holy_hand_grenade def test_hand_grenade(): mock_count =…
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
12
votes
3 answers

Python mock: wrap instance method

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 that I can track call_count on the method foo for…
Filip Kilibarda
  • 2,484
  • 2
  • 20
  • 31
12
votes
1 answer

How to mock a list in Python?

For example, I have some code which tries to access a list: def some_code(): script_dir = os.path.dirname(sys.argv[0]) I need to mock sys.argv[0]. So I add @patch.object to my test: import os import sys THIS_DIR =…
kinORnirvana
  • 1,667
  • 2
  • 17
  • 22
12
votes
1 answer

How do I mock the hierarchy of non-existing modules?

Let's assume that we have a system of modules that exists only on production stage. At the moment of testing these modules do not exist. But still I would like to write tests for the code that uses those modules. Let's also assume that I know how to…
ikostia
  • 7,395
  • 6
  • 30
  • 39
1 2
3
31 32