Fina Silva-Santisteban
07/30/2021, 9:02 PMdef do_something_1(params_1):
(does something)
File 2:
from path_to_file.file_1 import do_something_1
class SomethingTask(Task):
def run(self, params_1):
do_something_1(params_1)
do_something_2
File 3:
import unittest
from unittest.mock import patch
from path_to_file.file_2.SomethingTask import SomethingTask
@patch('path_to_file.file_1.do_something_1')
class TestSomethingTask(unittest.TestCase):
def test_1(self, mock_do_something_1):
task = SomethingTask()
task.run()
self.assertTrue(mock_do_something_1.called)
Instead of mocking do_something_1()
it actually calls do_something_1()
, and strangely the assertion returns False.Kevin Kho
Fina Silva-Santisteban
07/30/2021, 9:31 PMSomethingTask
was a regular python class and not a subclass from Task
, I could mock do_something_1()
in exactly that way. Could it have to do with the prefect run scope?Kevin Kho
from path_to_file.file_1 import do_something
in file 2 creates a new name for the object so it might be fixed if you do
import path_to_file.file_1
class SomethingTask(Task):
def run(self, params_1):
path_to_file.file_1.do_something_1(params_1)
do_something_2
because this will match the patch.
You might also be able to get this by patching @patch('path_to_file.file_2.do_something_1')
instead since it has a new name because of the from … import …
call, which would be similar logic to this response in the StackOverflow post.Fina Silva-Santisteban
07/30/2021, 9:55 PM@patch('path_to_file.file_2.SomethingTask.do_something_1')
Thanks so much for your help! 🙏