def __init__(self, hook_name): super(RegisteredHook, self).__init__() self.name = hook_name self._fail = False self.kwargs = {"a": 1, "b": 2, "c": 3} self.num_called = 0 self._dependency = None class HandlerException(Exception): pass self.exception_class = HandlerException def handler(**kw): assert kw == self.kwargs gossip.wait_for(self._dependency is None or self._dependency.called) if self._dependency is not None: assert self._dependency.called self.num_called += 1 self.last_timestamp = next(timestamp) if self._fail: raise self.exception_class() self.func = handler gossip.register(func=handler, hook_name=hook_name)
def test_set_strict_with_subgroups_and_tags(hook_name): def handler(): pass main_group_name = hook_name.partition(".")[0] gossip.register(handler, hook_name) with pytest.raises(UndefinedHook): gossip.get_or_create_group(main_group_name).set_strict() hook = gossip.define(hook_name, tags=("some_tag", "other_tag")) illegal_tags_hook = gossip.define(".".join([hook.group.full_name, "hook_with_illegal_tags"])) gossip.register(handler, illegal_tags_hook.full_name, tags=("fake_tag")) with pytest.raises(UnsupportedHookTags): gossip.get_or_create_group(main_group_name).set_strict() illegal_tags_hook.unregister_all() gossip.get_or_create_group(main_group_name).set_strict() with pytest.raises(UnsupportedHookTags): @gossip.register(hook_name, tags=("fake_tag",)) def handler2(): # pylint: disable=unused-variable pass with pytest.raises(UnsupportedHookTags): gossip.trigger_with_tags(hook_name, tags=("fake_tag",)) gossip.trigger_with_tags(hook_name, tags=("some_tag", "other_tag"))
def test_register_function_already_has_gossip_attr(hook_name): def func(): pass func.gossip = old_obj = object() gossip.register(func=func, hook_name=hook_name) assert func.gossip is old_obj
def test_no_error_hooks_called_on_success(suite): called = [] for hook_name in ['test_error', 'test_failure', 'test_skip', 'error_added']: gossip.register(lambda name=hook_name, **kw: called.append(name), 'slash.{}'.format(hook_name)) suite.run() assert not called
def test_no_error_hooks_called_on_success(suite): called = [] for hook_name in ['test_error', 'test_failure', 'test_skip', 'error_added']: gossip.register(lambda name=hook_name, **kw: called.append(name), 'slash.{0}'.format(hook_name)) suite.run() assert not called
def test_no_error_hooks_called_on_success(suite): called = [] for hook_name in ["test_error", "test_failure", "test_skip", "error_added"]: gossip.register(lambda name=hook_name, **kw: called.append(name), "slash.{0}".format(hook_name)) suite.run() assert not called
def test_token_unregister_from_hook(checkpoint, token, hook_name): # pylint: disable=unused-variable @gossip.register(hook_name, token=token) def handler(): gossip.unregister_token(token) gossip.register(hook_name)(checkpoint) gossip.trigger(hook_name) assert checkpoint.called
def test_token_unregister_from_hook_multiple_token_registrants(checkpoint, token, hook_name): # pylint: disable=unused-variable @gossip.register(hook_name, token=token) def handler(): gossip.unregister_token(token) gossip.register(hook_name)(checkpoint) # the second token registration should not be called! gossip.register(hook_name, token=token)(checkpoint) gossip.trigger(hook_name) assert checkpoint.num_times == 1
def test_token_unregister_from_hook_multiple_token_registrants( checkpoint, token, hook_name): # pylint: disable=unused-variable @gossip.register(hook_name, token=token) def handler(): gossip.unregister_token(token) gossip.register(hook_name)(checkpoint) # the second token registration should not be called! gossip.register(hook_name, token=token)(checkpoint) gossip.trigger(hook_name) assert checkpoint.num_times == 1
def test_register_with_bad_tags(tags, is_strict): if not is_strict: return def func(): pass with pytest.raises(gossip.exceptions.UnsupportedHookTags): func = gossip.register(_HOOK_NAME, tags=tags)(func)
def test_scope_management_with_hook_error_test_end(): """test_end errors are fatal, so the session abruptly stops. We just make sure we get the exception and that at least one test runs""" events = [] gossip.register('slash.test_end')(CustomException.do_raise) @slash.parametrize('param', range(10)) def test_something(param): events.append('test is running!') with slash.Session() as session: tests = slash.loader.Loader().get_runnables(test_something) assert tests with pytest.raises(CustomException) as caught: run_tests_in_session(tests, session=session) assert len(events) == 1 assert len(tests) > 1
def setup_hook(is_defined, is_strict, specify_tags, register_before_define, create_group_before_define): # note: the combination of register before define *and* create strict group # before define is guaranteed to create an error if register_before_define: if not is_strict or not create_group_before_define: gossip.register(_HOOK_NAME)(lambda: None) if create_group_before_define: _get_or_create_group(is_strict) if is_defined: if specify_tags: gossip.define(_HOOK_NAME, tags=_ACCEPTED_TAGS) else: gossip.define(_HOOK_NAME) if not create_group_before_define: _get_or_create_group(is_strict)
def test_before_cleanup_hook(request, forge): cleanup = forge.create_wildcard_function_stub(name="cleanup") before_cleanup_hook = forge.create_wildcard_function_stub(name="before_test_cleanup") test_end_hook = forge.create_wildcard_function_stub(name="test_end") gossip.register(before_cleanup_hook, "slash.before_test_cleanups") gossip.register(test_end_hook, "slash.test_end") before_cleanup_hook() cleanup() test_end_hook() forge.replay() def test_something(): slash.add_cleanup(cleanup) with slash.Session() as s: with s.get_started_context(): slash.runner.run_tests(make_runnable_tests(test_something))
def test_before_cleanup_hook(request, forge): cleanup = forge.create_wildcard_function_stub(name='cleanup') before_cleanup_hook = forge.create_wildcard_function_stub(name='before_test_cleanup') test_end_hook = forge.create_wildcard_function_stub(name='test_end') gossip.register(before_cleanup_hook, 'slash.before_test_cleanups') gossip.register(test_end_hook, 'slash.test_end') before_cleanup_hook() cleanup() test_end_hook() forge.replay() def test_something(): slash.add_cleanup(cleanup) with slash.Session() as s: with s.get_started_context(): slash.runner.run_tests(make_runnable_tests(test_something))
def test_register_class_and_instance_methods(): class MyClass(object): @gossip.register("hook") @classmethod def class_method(cls): pass @gossip.register("hook") @staticmethod def static_method(): pass def regular_method(self): pass m = MyClass() gossip.register("hook")(m.regular_method)
def test_scope_management_with_hook_error_test_end(): """test_end errors are fatal, so the session abruptly stops. We just make sure we get the exception and that at least one test runs""" events = [] gossip.register('slash.test_end')(CustomException.do_raise) num_tests = 10 @slash.parametrize('param', range(num_tests)) # pylint: disable=unused-argument def test_something(param): # pylint: disable=unused-argument events.append('test is running!') with slash.Session() as session: tests = slash.loader.Loader().get_runnables(test_something) assert tests run_tests_in_session(test_something, session=session) for result in session.results.iter_test_results(): assert len(result.get_errors()) == 1 assert result.get_errors()[0].exception_type is CustomException assert len(events) == num_tests assert len(session.results) == num_tests
def register(func): """A shortcut for registering hook functions by their names """ return gossip.register('slash.{0}'.format(func.__name__))(func)
def register(func): """A shortcut for registering hook functions by their names """ return gossip.register('slash.{}'.format(func.__name__))(func)