Skip to content
Advertisement

Tag: python-mock

How to unittest a datetime function

file.py test.py I get an Error: TypeError: ‘>’ not supported between instances of ‘MagicMock’ and ‘int’ Is there anyway to test this function? Probably the problem is with this part of the last line: (utc_date – namespace_created_date).days Answer I could reproduce and fix. Here is a working unittest file (assuming file.py is accessible): If I use date_now.datetime.utcnow.date.return_value = datetime.datetime(2022,1,1) I

How to mock subsequent function calls in python?

I’m new to testing and testing in python. I have a python class that looks like this : File name : my_hive.py I want to mock these functions : pyhive.hive.connect, pyhive.Connection.cursor(used by my class as hive.connect(hive_ip).cursor()) and pyhive.Cursor.execute (used by my class as self.cursor.execute(command) in execute method). I’m able to mock function call hive.connect and also I have been able

Pytest + mock: patch does not work without with clause

I’m testing complex logic that require joining of central fact table with 10-20 smaller dimensional tables. I want to mock that 10-20 smaller tables. How to patch methods return values in a for loop? See code below. tables.py: test_logic.py P.S. alternatively I can try to mock the BaseClass.load, but then I don’t know how to return the different data set

Python Mock Patch multiple methods in a class

Im trying to patch multiple methods in a class. Here is my simplified set up Hook.py is defined as HookTransfer.py defined as I want to mock the methods get_key and get_value in the Hook class. The following works i.e. prints New_Key and New_Value However this does not. It prints <MagicMock name=’Hook().get_key()’ id=’4317706896′> and <MagicMock name=’Hook().get_value()’ id=’4317826128′> Intuitively it seems like

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 PyPI module, so any import will fail). Currently, each time I try to

Mocking async call in python 3.5

How do I mock async call from one native coroutine to other one using unittest.mock.patch? I currently have quite an awkward solution: Then This works but looks ugly. Is there more pythonic way to do this? Answer The solution was actually quite simple: I just needed to convert __call__ method of mock into coroutine: This works perfectly, when mock is

Advertisement