Skip to content

Commit

Permalink
Scenario Names now uses Reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
medwards committed Jan 4, 2013
1 parent 5b9e49a commit 7a7b86c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 73 deletions.
2 changes: 2 additions & 0 deletions lettuce/plugins/dots.py
Expand Up @@ -18,6 +18,7 @@
import os
from lettuce import core
from lettuce.terrain import after
from lettuce.terrain import before
from lettuce.plugins.reporter import Reporter

class DotReporter(Reporter):
Expand All @@ -33,6 +34,7 @@ def print_scenario_ran(self, scenario):

reporter = DotReporter()

before.each_scenario(reporter.print_scenario_running)
after.each_scenario(reporter.print_scenario_ran)
after.each_step(reporter.store_failed_step)
after.all(reporter.print_end)
Expand Down
3 changes: 3 additions & 0 deletions lettuce/plugins/reporter.py
Expand Up @@ -15,6 +15,9 @@ def store_failed_step(self, step):
self.scenarios_and_its_fails[step.scenario] = step.why
self.failed_scenarios.append(step.scenario)

def print_scenario_running(self, scenario):
pass

def print_scenario_ran(self, scenario):
pass

Expand Down
88 changes: 21 additions & 67 deletions lettuce/plugins/scenario_names.py
Expand Up @@ -16,84 +16,38 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
from lettuce import core
from lettuce.terrain import after
from lettuce.terrain import before
from lettuce.plugins.reporter import Reporter

failed_scenarios = []
scenarios_and_its_fails = {}
class NameReporter(Reporter):
def print_scenario_running(self, scenario):
self.wrt('%s ... ' % scenario.name)

def print_scenario_ran(self, scenario):
if scenario.passed:
self.wrt("OK")
elif scenario.failed:
reason = self.scenarios_and_its_fails[scenario]
if isinstance(reason.exception, AssertionError):
self.wrt("FAILED")
else:
self.wrt("ERROR")
self.wrt("\n")

def wrt(what):
if isinstance(what, unicode):
what = what.encode('utf-8')
sys.stdout.write(what)
reporter = NameReporter()


@before.each_scenario
def print_scenario_running(scenario):
wrt('%s ... ' % scenario.name)


@after.each_scenario
def print_scenario_ran(scenario):
if scenario.passed:
print "OK"
elif scenario.failed:
reason = scenarios_and_its_fails[scenario]
if isinstance(reason.exception, AssertionError):
print "FAILED"
else:
print "ERROR"


@after.each_step
def save_step_failed(step):
if step.failed and step.scenario not in failed_scenarios:
scenarios_and_its_fails[step.scenario] = step.why
failed_scenarios.append(step.scenario)


@after.all
def print_end(total):
if total.scenarios_passed < total.scenarios_ran:
print # just a line to separate things here
for scenario in failed_scenarios:
reason = scenarios_and_its_fails[scenario]
wrt(str(reason.step))
wrt("\n")
wrt(reason.traceback)

wrt("\n")
word = total.features_ran > 1 and "features" or "feature"
wrt("%d %s (%d passed)\n" % (
total.features_ran,
word,
total.features_passed))

word = total.scenarios_ran > 1 and "scenarios" or "scenario"
wrt("%d %s (%d passed)\n" % (
total.scenarios_ran,
word,
total.scenarios_passed))

steps_details = []
for kind in "failed", "skipped", "undefined":
attr = 'steps_%s' % kind
stotal = getattr(total, attr)
if stotal:
steps_details.append("%d %s" % (stotal, kind))

steps_details.append("%d passed" % total.steps_passed)
word = total.steps > 1 and "steps" or "step"
wrt("%d %s (%s)\n" % (total.steps, word, ", ".join(steps_details)))
before.each_scenario(reporter.print_scenario_running)
after.each_scenario(reporter.print_scenario_ran)
after.each_step(reporter.store_failed_step)
after.all(reporter.print_end)


def print_no_features_found(where):
where = core.fs.relpath(where)
if not where.startswith(os.sep):
where = '.%s%s' % (os.sep, where)

wrt('Oops!\n')
wrt('could not find features at %s\n' % where)
reporter.wrt('Oops!\n')
reporter.wrt('could not find features at %s\n' % where)
14 changes: 8 additions & 6 deletions tests/functional/test_runner.py
Expand Up @@ -908,6 +908,7 @@ def test_output_level_2_fail():
assert_stdout_lines_with_traceback(
"See it fail ... FAILED\n"
"\n"
"\n"
"<Step: \"And this one fails\">\n"
"Traceback (most recent call last):\n"
' File "%(lettuce_core_file)s", line %(call_line)d, in __call__\n'
Expand Down Expand Up @@ -937,6 +938,7 @@ def test_output_level_2_error():
"It should pass ... OK\n"
"It should raise an exception different of AssertionError ... ERROR\n"
"\n"
"\n"
"<Step: \"Given my step that blows a exception\">\n"
"Traceback (most recent call last):\n"
' File "%(lettuce_core_file)s", line %(call_line)d, in __call__\n'
Expand Down Expand Up @@ -1233,10 +1235,10 @@ def just_pass(step, *args):
' I want to automate its test # tests/functional/bg_features/simple/simple.feature:4\n'
'\n'
' Background:\n'
' Given the variable "X" holds 2 # tests/functional/test_runner.py:1219\n'
' Given the variable "X" holds 2 # tests/functional/test_runner.py:1221\n'
'\n'
' Scenario: multiplication changing the value # tests/functional/bg_features/simple/simple.feature:9\n'
' Given the variable "X" is equal to 2 # tests/functional/test_runner.py:1219\n'
' Given the variable "X" is equal to 2 # tests/functional/test_runner.py:1221\n'
'\n'
'1 feature (1 passed)\n'
'1 scenario (1 passed)\n'
Expand Down Expand Up @@ -1268,12 +1270,12 @@ def just_pass(step, *args):
'\033[1;37m I want to automate its test \033[1;30m# tests/functional/bg_features/simple/simple.feature:4\033[0m\n'
'\n'
'\033[1;37m Background:\033[0m\n'
'\033[1;30m Given the variable "X" holds 2 \033[1;30m# tests/functional/test_runner.py:1254\033[0m\n'
'\033[A\033[1;32m Given the variable "X" holds 2 \033[1;30m# tests/functional/test_runner.py:1254\033[0m\n'
'\033[1;30m Given the variable "X" holds 2 \033[1;30m# tests/functional/test_runner.py:1256\033[0m\n'
'\033[A\033[1;32m Given the variable "X" holds 2 \033[1;30m# tests/functional/test_runner.py:1256\033[0m\n'
'\n'
'\033[1;37m Scenario: multiplication changing the value \033[1;30m# tests/functional/bg_features/simple/simple.feature:9\033[0m\n'
'\033[1;30m Given the variable "X" is equal to 2 \033[1;30m# tests/functional/test_runner.py:1254\033[0m\n'
'\033[A\033[1;32m Given the variable "X" is equal to 2 \033[1;30m# tests/functional/test_runner.py:1254\033[0m\n'
'\033[1;30m Given the variable "X" is equal to 2 \033[1;30m# tests/functional/test_runner.py:1256\033[0m\n'
'\033[A\033[1;32m Given the variable "X" is equal to 2 \033[1;30m# tests/functional/test_runner.py:1256\033[0m\n'
'\n'
'\033[1;37m1 feature (\033[1;32m1 passed\033[1;37m)\033[0m\n'
'\033[1;37m1 scenario (\033[1;32m1 passed\033[1;37m)\033[0m\n'
Expand Down

0 comments on commit 7a7b86c

Please sign in to comment.