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

Patching Class To Return AsyncMock Returns MagicMock

# a.py import BaseClass class ChildClass(BaseClass): async def do_something(): pass child_instance = ChildClass() async def method_a: await child_instance.do_something() @pytest.mark.asyncio async def test_method_a(): mock_class =…
0
votes
1 answer

How to mock a function which gets executed during the import time?

Here the ABC() and obj.print_1() get called during the import time and it prints "making object" and "printed 1" respectively. How can we mock all the three functions, __init__(), print_1(), and print_2()? xyz.py from abc import ABC obj =…
Amogh Mishra
  • 1,088
  • 1
  • 16
  • 25
0
votes
1 answer

How to mock a function of a class in an imported module but outside the scope of a function?

I am trying to figure out how to use the @patch.object to mock a __init__ and, log.write() for the Logger class, which is imported but isn't used inside the function of a module. The tuorial such as this,…
0
votes
0 answers

Mocking a factory function doesn't honor autospec

I'm using: platform darwin -- Python 3.9.13, pytest-7.2.0, pluggy-1.0.0 plugins: mock-3.10.0, socket-0.5.1 pywikibot 7.7.1 I want to mock pywikibot.Site. Surprisingly, this test passes: def test_site_autospec(mocker, capsys): mock_Site =…
Roy Smith
  • 2,039
  • 3
  • 20
  • 27
0
votes
1 answer

Python Mock: mock calls counted by call_count

Is there a way to get the mock calls counted by call_count in this example: from mock import MagicMock mock = MagicMock() mock.get_query(arg=1).execute() mock.get_query(arg=2).execute() print('count:', mock.get_query.call_count) print('calls:',…
Jerther
  • 5,558
  • 8
  • 40
  • 59
0
votes
1 answer

Python unittest create mock class

The Case: I've a class View in my code that creates an instance during execution, which one I want to mock. I'm passing implementation of View before running the code. class View: def __init__(arg): self.arg = arg …
salius
  • 918
  • 1
  • 14
  • 30
0
votes
1 answer

How do I test a function which calls an input function?

I want to test a function that calls another function that only asks for input and returns True or False. I have a function that deletes a booking from a CSV file like so: def delete_booking(self, booking_id): if…
0
votes
1 answer

Python unittest returns MagicMock object instead of value

I have a program like this: class SomeClass: _attribute = None def __init__(self, attribute): self._attribute = attribute @property def attribute(self): print("abc") …
0
votes
0 answers

how do patch the subprocess.run function in python while importing just "run"?

I imported run from subprocess and I'd like to patch the call to run. Is it possible to do this without having to import the entire subprocess package? from subprocess import run run([some_command]) patch.object(subprocess, 'run',…
dataviews
  • 2,466
  • 7
  • 31
  • 64
0
votes
1 answer

Can't listen to print function in __main__.py with unittest.mock

I am trying to listen to the print function from __main__.py. I use the patch function from unittest.mock. def main_tester(command): fake_command() capturedoutput = io.StringIO() sys.stdout = capturedoutput with patch('sys.argv',…
0
votes
1 answer

Python mock patch not mocking the object

I am using the python mocking library and I am not sure why I get this result. Why only the second one got mocked and not the first one? What's the best way to do it? import unittest from unittest.mock import patch, Mock import requests from…
Philippe Remy
  • 2,967
  • 4
  • 25
  • 39
0
votes
1 answer

Test (unittest, mock) if a global variable list is updated

file.py: GLOB_VAR = [] def my_func(x, msg): if x: logging.warning(msg) GLOB_VAR.append(x) test.py: @patch('file.GLOB_VAR', []) @patch('logging.Logger.warning') def test_file_with_msg(self, logging): x = 'test_x' msg =…
0
votes
0 answers

Python Mock Instantiation of Object

Could someone please let me know how to mock the DatabaseAPI object for below unit test case example. Getting error at getURL in test_load.py file. File load.py from database_api import DatabaseAPI Database_api_client = DatabaseAPI(username,…
Veds
  • 11
  • 1
  • 6
0
votes
0 answers

What is the proper way to Mock or MagicMock logging in Python unit tests?

When unit testing Python code with the unittest standard library, what is the proper way to Mock or MagicMock logging from the code that is being tested? Would the approach outlined in the stripped down, simplified example below be considered…
Myklebost
  • 59
  • 8
0
votes
1 answer

UnitTesting: Mock pyodbc cursor messages

I've been trying to test the below metheod specially the if block and have tried multiple things like patching, mocking in various combinations of pyodbc but I've not been able to mock the if condition. def execute_read(self, query): dbconn =…