diff --git a/mamba/example_group.py b/mamba/example_group.py index 01a51c5..22dcc8d 100644 --- a/mamba/example_group.py +++ b/mamba/example_group.py @@ -67,7 +67,7 @@ def execute_hook(self, hook, execution_context): if hook.endswith('_all') and not self.hooks.get(hook): return - if self.parent is not None: + if not hook.endswith('_all') and self.parent is not None: self.parent.execute_hook(hook, execution_context) for registered in self.hooks.get(hook, []): diff --git a/spec/hooks_spec.py b/spec/hooks_spec.py index 5ebd2f3..44b7803 100644 --- a/spec/hooks_spec.py +++ b/spec/hooks_spec.py @@ -52,35 +52,43 @@ expect(self.execution_context.calls).to(equal(['example_1', 'example_2', 'after_all'])) with context('when having nested contexts'): - with it('executes only once the before_all hooks'): + with it('executes each before_all hook only once'): self.parent.hooks['before_all'] = [lambda ctx: ctx.calls.append('before_all_parent')] child = an_example_group() child.append(Example(lambda ctx: ctx.calls.append('example'))) + child.hooks['before_all'] = [lambda ctx: ctx.calls.append('before_all_child_1')] self.parent.append(child) child = an_example_group() child.append(Example(lambda ctx: ctx.calls.append('example_2'))) + child.hooks['before_all'] = [lambda ctx: ctx.calls.append('before_all_child_2')] self.parent.append(child) self.parent.execute(self.reporter, self.execution_context) - expect(self.execution_context.calls).to(equal(['before_all_parent', 'example', 'example_2'])) + expect(self.execution_context.calls).to(equal( + ['before_all_parent', 'before_all_child_1', 'example', 'before_all_child_2', 'example_2'] + )) - with it('executes only once the after_all hooks'): + with it('executes each after_all hook only once (in opposite nesting order)'): self.parent.hooks['after_all'] = [lambda ctx: ctx.calls.append('after_all_parent')] child = an_example_group() child.append(Example(lambda ctx: ctx.calls.append('example'))) + child.hooks['after_all'] = [lambda ctx: ctx.calls.append('after_all_child_1')] self.parent.append(child) child = an_example_group() child.append(Example(lambda ctx: ctx.calls.append('example_2'))) + child.hooks['after_all'] = [lambda ctx: ctx.calls.append('after_all_child_2')] self.parent.append(child) self.parent.execute(self.reporter, self.execution_context) - expect(self.execution_context.calls).to(equal(['example', 'example_2', 'after_all_parent'])) + expect(self.execution_context.calls).to(equal( + ['example', 'after_all_child_1', 'example_2', 'after_all_child_2', 'after_all_parent'] + )) with it('executes first the before_all and later the before_each for every parent, in declaration order'): self.parent.hooks['before_all'] = [lambda ctx: ctx.calls.append('before_all_parent')]