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

How to mock a method call in a global variable

I was checking this trying to assign a method call in a module-level variable with the intention of being executed only once, but not sure why before running any of my unit tests it will pass through all the global references of the module, the…
jam
  • 489
  • 1
  • 4
  • 19
0
votes
0 answers

Unittest mock patch not creating a mock for method

Here is the proj structure that I have - src/ package1/ __init__.py file1.py # has the method -> _method_name tests/ __init__.py test_file1.py I am using unittest to test my code. I want to mock a method…
Dan
  • 79
  • 10
0
votes
0 answers

python mock patch original function still being called

Kinda new to python unittest mock. I have the following code which mocks a number of functions, then tests and calls main(). The mock items are called from main. However, even though the unitests run successfully the original functions are being…
Saifudeen
  • 3
  • 2
0
votes
1 answer

unittest @patch return first tuple in list of the tuple

I have a problem with unittest. The patched method does not return a list of tuples, but only one tuple: My code from date import Date from ditreport import DIT_report from unittest import TestCase from unittest.mock import patch, Mock def…
Vlad Kn
  • 141
  • 2
  • 13
0
votes
1 answer

Pytest with mock.patch does not mock os.path.curdir

I'm trying to mock os.path.curdir so it returns given return_value but this does not work. from os.path import curdir from unittest import TestCase from unittest.mock import patch from hello import Hello class Hello: def hey(self): …
Meroz
  • 859
  • 2
  • 8
  • 28
0
votes
1 answer

Raise error when calling a non-existent method of a mocked method of a mock created from `spec`

from unittest import mock class A: def f(self): pass m = mock.MagicMock(spec_set=A) m.f.called_once() # I want this to fail Out[42]: I have made a mistake in my unit test and called…
Anton Daneyko
  • 6,528
  • 5
  • 31
  • 59
0
votes
1 answer

Using a generator with Python mock to replicate server responses

I would like to use a list (converted into a generator) to serve as a mock for my API calls (using unittest.mock). My function is: def monitor_order(order_id) order_info = client.get_order_status(order_id) order_status =…
Jayjay Jay
  • 57
  • 1
  • 11
0
votes
1 answer

python mocks a function properly, however the call count is not accounted

I have a class like the below: Class a: def fn1(self): p1=multiprocessing.Process(self.fn2, (arg1,) p1.start() p1.join() def fn2(self, arg1): … I am trying to test it and I have mocked the self.fn2…
0
votes
1 answer

Print statement executing but function call directly after failing assert_called

tl;dr: I know a code block I am testing with unittest is being executed because of a print statement, but a function directly after the print statement is failing an assert_called check. When running the code, the function is being called and…
0
votes
1 answer

Cannot mock subprocess.check_call

I want to write a test for this function while mocking check_call (I don't want it to be called): from subprocess import check_call def foo_method(): check_call(["ls"]) print("hello") This is the test: import unittest.mock from scripts…
T.Poe
  • 1,949
  • 6
  • 28
  • 59
0
votes
1 answer

Python Mock patch ldap3 search response

I am trying to mock the below function but I'm not sure how to mock the Connection response: def get_user_res(user, pass): res = None server = Server('my_server') connnection = Connection(server, user, pass, strategy=SAFE_SYNC,…
0
votes
1 answer

Reference a nested function in unittest assert_called_with()

How can I reference nested functions in MagicMock..assert_called_with()? I want to mock an object (called here EventObject) that can distribute events to installed event handlers. The signature for handler installation looks like this: class…
0
votes
0 answers

Unit Testing in Python Djnago

I am using Django==3.0.3 djangorestframework==3.11.0 python ==3.6.8 I want to write unit testing for API and no need to use any database(no need of mock database) that mean I want to mock the database call and write unit test cases how can i write…
0
votes
0 answers

How to remove patched dependency in python unit tests

Given the following unit test @patch("requests.get") def test_create_resource_success(self, request_get): request_get_mock.return_value = Mock(ok=True) request_get_mock.return_value.json.return_value = {'key': 'value'} success =…
alegria
  • 1,290
  • 14
  • 23
0
votes
1 answer

How to mock and test decorator?

How to test the following decorator which calls 3rd party library? import third_party_lib import functools class MyDecorator: def __init__(self, ....): self.third_party_lib = ThirdPartyLib(....) # will create a 3rd party instance …
ca9163d9
  • 27,283
  • 64
  • 210
  • 413