Python新手求助:如何测试方法中实例对象的方法调用正确性
register and push are called correctly in dispatch_events Hey there! No worries at all about basic questions—we all start somewhere 😊 Testing method calls on dependencies is a super common scenario, and Python's standard library has great tools for this with unittest.mock. Let me walk you through how to write a test case for your dispatch_events function.
Key Idea: Mock the Dispatcher Class
Instead of using the real Dispatcher (which might have dependencies or not be fully implemented yet), we'll create a mock version of it. This lets us track exactly when its methods are called, and with what arguments.
Test Case with unittest (Standard Library)
If you're using Python's built-in unittest framework, here's how to structure your test:
import unittest from unittest.mock import patch, MagicMock from your_module import dispatch_events # Replace with your actual module name class TestDispatchEvents(unittest.TestCase): @patch('your_module.Dispatcher') # Important: Patch the Dispatcher as imported in your_module def test_dispatch_events_calls_register_and_push(self, mock_dispatcher): # Create a test event to pass to the function test_event = {"event_type": "sample", "payload": "test_data"} # Call the function we want to test dispatch_events(test_event) # 1. Verify that a Dispatcher instance was created (optional but good to check) mock_dispatcher.assert_called_once() # Get the mocked Dispatcher instance that was created dispatcher_instance = mock_dispatcher.return_value # 2. Verify register was called exactly once with "TopicOne" dispatcher_instance.register.assert_called_once_with("TopicOne") # 3. Verify push was called exactly once with our test event dispatcher_instance.push.assert_called_once_with(test_event) if __name__ == '__main__': unittest.main()
Test Case with pytest (Popular Third-Party Framework)
If you prefer using pytest (with pytest-mock), the code is a bit more concise:
from your_module import dispatch_events def test_dispatch_events_calls_register_and_push(mocker): # Mock the Dispatcher class mock_dispatcher = mocker.patch('your_module.Dispatcher') # Create a test event test_event = {"event_type": "sample", "payload": "test_data"} # Run the function dispatch_events(test_event) # Validate instance creation mock_dispatcher.assert_called_once() # Get the mocked instance dispatcher_instance = mock_dispatcher.return_value # Check method calls dispatcher_instance.register.assert_called_once_with("TopicOne") dispatcher_instance.push.assert_called_once_with(test_event)
Important Notes
- Patch Path: Make sure the path you pass to
@patchormocker.patchmatches whereDispatcheris imported in the module containingdispatch_events. For example, if your code hasfrom my_dispatchers import Dispatcher, you'd patch'your_module.Dispatcher'(not'my_dispatchers.Dispatcher'). - Mock Instance:
mock_dispatcher.return_valuegives you access to the mocked Dispatcher object thatdispatch_eventscreates when it runsDispatcher(). This is where we check for calls toregisterandpush. - No Real Implementation Needed: This test works even if
Dispatcherisn't fully implemented yet—we're only testing thatdispatch_eventsinteracts with it correctly.
Hope this helps you get started with Python testing! Feel free to ask if you need to clarify any part of this.
内容的提问来源于stack exchange,提问作者ediblecode

