How to unittest the sequence of function calls made inside a python fuction?

2024/11/15 10:58:35

I would like to unittest a fuction and assert if the sequence of function calls made inside the function workflow(). Something like,

      [1st called] fetch_yeargroup_ls()[2nd called] invoke_get_links()....... 

I searched across many discussions but never found one answering my question.

Answer

If you are using mock you can create mocks as attributes of a parent mock, when patching out those functions:

try:# Python 3from unittest.mock import MagicMock, patch, call
except ImportError:# Python 2, install from PyPI firstfrom mock import MagicMock, patch, call
import unittestfrom module_under_test import function_under_testclass TestCallOrder(unittest.TestCase):def test_call_order(self):source_mock = MagicMock()with patch('module_under_test.function1', source_mock.function1), \patch('module_under_test.function2', source_mock.function2), \patch('module_under_test.function3', source_mock.function3)# the test is successful if the 3 functions are called in this# specific order with these specific arguments:expected = [call.function1('foo'),call.function2('bar'),call.function3('baz')]# run your code-under-testfunction_under_test()self.assertEqual(source_mock.mock_calls, expected)

Because the 3 functions are attached to source_mock, all calls to them are recorded on the parent mock object in the Mock.mock_calls attribute and you can make assertions about their call order.

I attached the 3 function mocks simply by looking them up as attributes on the source_mock object, but you could also use the Mock.attach_mock() method to attach mocks you created in a different way to a parent.

https://en.xdnf.cn/q/71896.html

Related Q&A

OpenCV: Extract SURF Features from user-defined keypoints

I want to compute SURF Features from keypoints that I specify. I am using the Python wrapper of OpenCV. The following is the code I am trying to use, but I cannot find a working example anywhere.surf…

Realtime processing and callbacks with Python and C++

I need to write code to do some realtime processing that is fairly computationally complex. I would like to create some Python classes to manage all my scripting, and leave the intensive parts of the a…

How can I troubleshoot a segmentation fault when working with Python Ctypes and C++?

Lets say I have the following two function signatures in C++:BYTE* init( BYTE* Options, BYTE* Buffer )and:int next( BYTE* interface, BYTE* Buffer )The idea is that I first initialize an Interface class…

Undefined variable from import when using protocol buffers in PyDev

Ive got a PyDev project that uses protocol buffers. The protocol buffer files are located in a zip file generated by the protoc compiler. Everything works when I run the program, however PyDev reports …

Animating a network graph to show the progress of an algorithm

I would like to animate a network graph to show the progress of an algorithm. I am using NetworkX for graph creation. From this SO answer, I came up with a solution using clear_ouput from IPython.displ…

How to run grpc on ipv4 only

Im going to run a grpc server on IPv4 address like this: server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) protoc_pb2_grpc.add_ProtocServicer_to_server(StockProtocServicer(), server) ser…

Python/PyCharm mark unused import as used

I need to import a resource_rc.py file in my module. It is immediately marked by PyCharm as "unused". Is there a way to mark "unused" imports and also variables, etc. as used in Pyt…

Replacing every 2nd element in the list

I got a 2 dimensional list:[[5, 80, 2, 57, 5, 97], [2, 78, 2, 56, 6, 62], [5, 34, 3, 54, 6, 5, 2, 58, 5, 61, 5, 16]]In which I need to change every second element to 0, starting from first one. So it s…

Are C++-style internal typedefs possible in Cython?

In C++ its possible to declare type aliases that are members of a class or struct:struct Foo {// internal type aliastypedef int DataType;// ... };Is there any way to do the same thing in Cython? Ive t…

How do I use a regular expression to match a name?

I am a newbie in Python. I want to write a regular expression for some name checking. My input string can contain a-z, A-Z, 0-9, and _ , but it should start with either a-z or A-Z (not 0-9 and _ ). I…