Successfully testing the pyinotify module?

I use pyinotify to mirror files from source directory to destination directory. My code works when I execute it manually, but it's hard for me to get accurate unit test results. I think the problem boils down to the following:

  • I need to use the ThreadedNotifier in my tests, otherwise they will just hang, waiting for manual input.
  • Since I'm using a different thread, my tests and Notifier are out of sync. Tests that pass during the observation, manual tests do not work when performing unit tests.

Has anyone succeeded in unit testing pyinotify?

+3
source share
1 answer

Unit testing should usually take into account things such as streams and the file system. Do you have a reason for unit test with actual file system, user input, etc.?

Python makes a monkey patch very easy; you can, for example, replace the entire os / sys module with a mock object (e.g. Python Mock ) so that you never have to deal with the file system. It will also make your tests faster.

, , , , . , .. .

Edit

, "" .

, , my_module, get_text_upper:

def get_text_upper(filename):
    return open(filename).read().upper()

( , , ...). open, StringIO:

from cStringIO import StringIO

def fake_open(text):
    fp = StringIO()
    fp.write(text)
    fp.seek(0)
    return fp

def test_get_text():
    my_module.open = lambda *args, **kwargs : fake_open("foo")
    text = my_module.get_text_upper("foo.txt")
    assert text == "FOO", text

.

fooobar.com/questions/64246/....

+5

Source: https://habr.com/ru/post/1767820/


All Articles