Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #261: calls before/after hooks for each outline example. #437

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/reference/terrain.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ by the fact that its ran *after* lettuce run the feature.
@before.each_scenario
=====================

This hook is ran before lettuce run each scenario
This hook is ran before lettuce run each scenario or each example of an
scenario outline.

The decorated function takes a :ref:`scenario-class` as parameter, so
that you can use it to fetch steps inside.
Expand All @@ -308,7 +309,8 @@ that you can use it to fetch steps inside.
====================

This hooks behaves in the same way @before.each_scenario does, except
by the fact that its ran *after* lettuce run the scenario.
by the fact that its ran *after* lettuce run the scenario or each example
of an scenario outline.

.. highlight:: python

Expand Down
17 changes: 13 additions & 4 deletions lettuce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,14 @@ def run(self, ignore_case, failfast=False):
before_each and after_each callbacks for steps and scenario"""

results = []
call_hook('before_each', 'scenario', self)

def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False):
self.outline = outline
self.outline_index = order
self.is_outline = bool(outline)

call_hook('before_each', 'scenario', self)

try:
if self.background:
self.background.run(ignore_case)
Expand All @@ -734,9 +739,13 @@ def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False):
call_hook('outline', 'scenario', self, order, outline,
reasons_to_fail)

skip = lambda x: x not in steps_passed and x not in steps_undefined and x not in steps_failed
skip = lambda x: (x not in steps_passed and
x not in steps_undefined and
x not in steps_failed)
steps_skipped = filter(skip, all_steps)

call_hook('after_each', 'scenario', self)

return ScenarioResult(
self,
steps_passed,
Expand All @@ -747,11 +756,11 @@ def run_scenario(almost_self, order=-1, outline=None, run_callbacks=False):

if self.outlines:
for index, outline in enumerate(self.outlines):
results.append(run_scenario(self, index, outline, run_callbacks=True))
results.append(run_scenario(
self, index, outline, run_callbacks=True))
else:
results.append(run_scenario(self, run_callbacks=True))

call_hook('after_each', 'scenario', self)
return results

def _add_myself_to_steps(self):
Expand Down
2 changes: 2 additions & 0 deletions lettuce/plugins/colored_shell_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def print_scenario_running(scenario):
# We haven't seen this background before, add our 1st scenario
world.background_scenario_holder[scenario.background] = scenario
return
if scenario.is_outline and scenario.outline_index > 0:
return
string = scenario.represented()
string = wrap_file_and_line(string, '\033[1;30m', '\033[0m')
write_out("\n\033[1;37m%s" % string)
Expand Down
2 changes: 2 additions & 0 deletions lettuce/plugins/shell_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def print_scenario_running(scenario):
# We haven't seen this background before, add our 1st scenario
world.background_scenario_holder[scenario.background] = scenario
return
if scenario.is_outline and scenario.outline_index > 0:
return
wrt('\n')
wrt(scenario.represented())

Expand Down
8 changes: 4 additions & 4 deletions tests/functional/test_after_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ def run_feature(feature, feature_will_fail, failfast,

@with_setup(prepare_stdout)
def test_success_outline():
run_feature('success_outline', False, False, 1, 1, 24, 3)
run_feature('success_outline', False, False, 1, 3, 24, 3)

@with_setup(prepare_stdout)
def test_success_outline_failfast():
run_feature('success_outline', False, True, 1, 1, 24, 3)
run_feature('success_outline', False, True, 1, 3, 24, 3)

@with_setup(prepare_stdout)
def test_fail_outline():
run_feature('fail_outline', True, False, 1, 1, 24, 3)
run_feature('fail_outline', True, False, 1, 3, 24, 3)

@with_setup(prepare_stdout)
def test_fail_outline_failfast():
run_feature('fail_outline', True, True, 1, 1, 12, 2)
run_feature('fail_outline', True, True, 1, 2, 12, 2)

@with_setup(prepare_stdout)
def test_success_non_outline():
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/test_subunit_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
import logging
from cStringIO import StringIO

from nose.tools import with_setup, assert_equal
Expand Down Expand Up @@ -249,6 +250,12 @@ def test_subunit_output_undefined_steps():
'steps': ContentContains('? When this test step is undefined\n'),
}),
}),
Includes({
'status': 'fail',
'details': Includes({
'steps': ContentContains('? When this test step is undefined\n'),
}),
}),
]

runner = Runner(feature_name('undefined_steps'), enable_subunit=True)
Expand Down