millions-night-34157
07/01/2024, 2:00 PMAssertionError: Expected 'mock' to be called once. Called 0 times.
error. What could be the possible reason? Code and test is given below:
def my_flyte_task(config: Config) -> str:
# logic to do something with config and return a string called "my_path"
def my_workflow(config: Config) -> str:
task_result = my_flyte_task(config=config)
return task_result
@flyte_patch(my_flyte_task):
def test_my_wf(mock_my_flyte_task):
mock_my_flyte_task.return_value = "mocked_path"
config = CreateConfig()
wf_result = my_wf(config=config)
mock_my_flyte_task.assert_called_once()
ancient-wolf-19325
07/01/2024, 2:00 PMAssertionError: Expected 'mock' to be called once. Called 0 times.
error indicates that the mock object mock_my_flyte_task
was not called during the test execution. This could be due to several reasons:
1. Incorrect Function Call: Ensure that my_flyte_task
is correctly called within my_workflow
.
2. Patch Scope: Ensure that the patch is applied correctly and the function being tested is within the scope of the patch.
Based on Flyte's mocking documentation, here's a possible fix:
from flytekit.testing import patch
from unittest.mock import MagicMock
def my_flyte_task(config: Config) -> str:
# logic to do something with config and return a string called "my_path"
return "my_path"
def my_workflow(config: Config) -> str:
task_result = my_flyte_task(config=config)
return task_result
@patch(my_flyte_task)
def test_my_wf(mock_my_flyte_task: MagicMock):
mock_my_flyte_task.return_value = "mocked_path"
config = CreateConfig()
wf_result = my_workflow(config=config)
assert wf_result == "mocked_path"
mock_my_flyte_task.assert_called_once()
Ensure that:
- my_flyte_task
is correctly imported and used in my_workflow
.
- The patch decorator is correctly applied to my_flyte_task
.
Would you like more detail on any specific part?
Sources:
- Flyte Mocking Documentation
- Flyte Discussion on Unit Testing 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.