I'm having a class with watchdog and i want to create a unit test that check that at a call to a method in my class, methods of watchdog.Observer calls occur
this is the class i have
from watchdog.observers.polling import PollingObserver as Observer
class FolderObserver:
def __init__(self):
self.__event_handler = MyObserver()
self.__event_observer = Observer()
def watch(self, path):
self.start(path)
try:
while True:
time.sleep(1)
except:
self.stop()
def start(self, path):
self.__schedule(path)
self.__event_observer.start()
def stop(self):
self.__event_observer.stop()
self.__event_observer.join()
def __schedule(self, path):
self.__event_observer.schedule(self.__event_handler, path, recursive=True)
and this is a unit test I tried
class TestWatch(TestCase):
"""Unit test for loadConfig function"""
@patch.object(watchdog.observers.polling.PollingObserver,'schedule')
@patch.object(watchdog.observers.polling.PollingObserver,'start')
def test_start_calls_schedule(self, start, schedule):
src_path = "random_path"
observer = FolderObserver()
observer.start(src_path)
start.assert_called_with(mockObserver, src_path, recursive=True)
schedule.assert_called_with(mockObserver, src_path, recursive=True)
and I cannot get it to work,
what am I doing wrong?
You need to patch the class where it is imported, and use Observer
since you're using an alias, so:
@patch.object({path.containing.FolderObserver}.Observer,'schedule')
@patch.object({path.containing.FolderObserver}.Observer,'start')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.