From 12e50ecc3b39944b617b2e0011891e4b7cb0e1bd Mon Sep 17 00:00:00 2001 From: Friedrich Weber Date: Tue, 9 Jan 2018 21:37:22 +0100 Subject: [PATCH] First prototype for integration tests Working on #32 --- requirements.txt | 1 + test/mockdialog.py | 47 +++++++++++++++++++++++++++++++++++ test/test_menu_privacyidea.py | 15 +++++++++++ 3 files changed, 63 insertions(+) create mode 100644 test/mockdialog.py create mode 100644 test/test_menu_privacyidea.py diff --git a/requirements.txt b/requirements.txt index eecb7bf..fd41a9a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ pyparsing==2.0.3 python2-pythondialog==3.0.1 configobj==5.0.6 six==1.10.0 +mock==2.0.0 diff --git a/test/mockdialog.py b/test/mockdialog.py new file mode 100644 index 0000000..f32e4b1 --- /dev/null +++ b/test/mockdialog.py @@ -0,0 +1,47 @@ +from contextlib import contextmanager +from functools import partial + +import mock +import dialog + +PATCH_FUNCTIONS = ('yesno',) + +class UserBehavior(object): + def __init__(self): + self._handlers = [] + + def expect(self, + function_name, + return_value, + callback=None): + def handler(actual_function_name, *args, **kwargs): + assert actual_function_name == function_name,\ + "Expected call of {}, got {}!".format(function_name, actual_function_name) + if callback is not None: + callback(*args, **kwargs) + return return_value + self._handlers.append(handler) + + def expect_yesno(self, answer): + return_value = dialog.Dialog.OK if answer else dialog.Dialog.CANCEL + self.expect('yesno', return_value) + + def _side_effect_handler(self, function_name, *args, **kwargs): + assert self._handlers, "Got call of {}, but no handlers are defined!".format(function_name) + handler = self._handlers.pop() + return handler(function_name, *args, **kwargs) + + def _start_mock(self, function_name): + target = 'dialog.Dialog.{}'.format(function_name) + patcher = mock.patch(target, side_effect=partial(self._side_effect_handler, function_name)) + patcher.start() + return patcher + + @contextmanager + def simulate(self): + patchers = [self._start_mock(function) for function in PATCH_FUNCTIONS] + try: + yield + finally: + for patcher in patchers: + patcher.stop() \ No newline at end of file diff --git a/test/test_menu_privacyidea.py b/test/test_menu_privacyidea.py new file mode 100644 index 0000000..c4aa2a9 --- /dev/null +++ b/test/test_menu_privacyidea.py @@ -0,0 +1,15 @@ +import unittest +from dialog import Dialog + +import mock + +from mockdialog import UserBehavior + +class TestMenuPrivacyIDEA(unittest.TestCase): + def test_invocation(self): + user = UserBehavior() + user.expect('yesno', Dialog.CANCEL) + with user.simulate(): + d = Dialog() + code = d.yesno('hello!', width=70) + print 'code is ', code