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
1
vote
0 answers

Why patched method of the class behaves differently from the patched method of the patched class?

I am playing with patches and mocks and trying to understand the difference between patch of the method of a class and patch of the method of patched class. In both cases I create class instance and try to patch the bound method by bounded one…
Andriy
  • 1,270
  • 3
  • 17
  • 35
1
vote
0 answers

How do I patch a python dict such that it returns mutliple values?

Python's unittest.mock allows me to patch a function such that it returns multiple values: m = Mock(side_effect=["myName", 100, 200]) Calling m() multiple times will then return "myName", 100 and finally 200. I can also patch a dict with patch.dict…
oz123
  • 27,559
  • 27
  • 125
  • 187
1
vote
1 answer

unittest.mock doesn't work when tests are run with pytest

I have the following project structure: tests/ └── messenger/ └── messaging_test.py app/ ├── __init__.py ├── models.py └── messenger/ ├── __init__.py └── messaging.py Inside messaging.py module there's a function send_schedule with the…
Pavel Vergeev
  • 3,060
  • 1
  • 31
  • 39
1
vote
1 answer

Mock requests.json after getting response in Python

I have test: class MyTests(TestCase): def setUp(self): self.myclient = MyClient() @mock.patch('the_file.requests.json') def test_myfunc(self, mock_item): mock_item.return_value = [ {'itemId': 1}, …
codyc4321
  • 9,014
  • 22
  • 92
  • 165
1
vote
2 answers

Mock an attribute in python class for unit testing

I would like to mock the self.var in my unit test since the name server lookup fails. I've tried bunch of things using patch, but I couldn't get it to work. What is the best way to mock self.var? My class definition is as below: class A: def…
leopoodle
  • 2,110
  • 7
  • 24
  • 36
1
vote
1 answer

How to patch method io.RawIOBase.read with unittest?

I've recently learned about unittest.monkey.patch and its variants, and I'd like to use it to unit test for atomicity of a file read function. However, the patch doesn't seem to have any effect. Here's my set-up. The method under scrutiny is roughly…
1
vote
1 answer

near-useless assertion output from hamcrest.contains_inanyorder applied to unittest.mock.Mock.mock_calls

Often, I care about the exact calls the system under test makes to another part of the software (which I mock in the test), but not about the order, in which those calls happen. (E.g. because the end effect on the real other part replaced by the…
das-g
  • 9,718
  • 4
  • 38
  • 80
1
vote
1 answer

Doubling objects in unittest.mock

I am trying to use unittest with unittest.mock to do some tests in my application. I have two classes MainClass and Caller. I would like to test main class with a double Caller. This is in short what I have: class MainClass: def __init__(self,…
Ela
  • 3,142
  • 6
  • 25
  • 40
1
vote
3 answers

Python patch mock appears to be called, but assert fails

I'm using Python 2.6.6 What I'm trying to do is replace the creation of an object with a Mock, to make sure that the correct calls are made. Should be straightforward. My Module: import dir.SubModule class Cls( object ): def Start( self ): …
1
vote
0 answers

How to access TestCase instance method from Mock instance method?

TL;DR Can I access the current unittest.TestCase instance from within a method of a mock class without explicitly passing in that instance? If not, what is the proper way to access the assertion helper functions of TestCase (which are instance…
das-g
  • 9,718
  • 4
  • 38
  • 80
0
votes
0 answers

How to mock input and print with concurrent.futures.ProcessPoolExecutor?

I am trying to use unittest to mock input and print with concurrent.futures.ProcessPoolExecutor, but I get EOFerror everytime it runs. It works perfectly with concurrent.futures.ThreadPoolExecutor, but not with processes. Here is my code: import…
0
votes
0 answers

MagickMock() assert_called not recognizing calls

i have a function like this, stored in file example.py: def my_func(file): conn = get_connection() conn.upload_file(file) conn.execute_file(file) Now, i want to test it, so i`m using MagicMock in test_example.py…
Ni3dzwi3dz
  • 177
  • 8
0
votes
0 answers

Mocking Functions with Complex Module Structures

I have a function funcA that lives in folderA.folderB.foo.py def funcA(): ... return 0 if (some condition) else 1 That is then used in classB.funcB in folderA.folderC.bar.py. from folderA.folderB.foo import funcA class B: def…
0
votes
0 answers

unittest for decorator in python

class BackgroundProcess: def background(f): from functools import wraps @wraps(f) def wrapped(*args, **kwargs): loop = asyncio.get_event_loop() if callable(f): return…
Vishnuraj
  • 11
  • 2
0
votes
1 answer

Issue with invoking Mock in PyCharm

I have taken up Python recently. I'm trying to use unittest.Mock() for testing my code in PyCharm. But, PyCharm fails with the error: requests = Mock() TypeError: 'module' object is not callable My program: import requests from unittest import…
marie20
  • 723
  • 11
  • 30