Skip to content

Commit a26ce26

Browse files
Merge pull request #196 from thedrow/ISSUE-139-gherkin-terminal-reporter
Merged version of the gherkin reporter
2 parents 6cd65b6 + 3d8447a commit a26ce26

File tree

7 files changed

+435
-2
lines changed

7 files changed

+435
-2
lines changed

CHANGES.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
Changelog
22
=========
33

4+
2.18.0
5+
------
6+
7+
- Add gherkin terminal reporter (spinus + thedrow)
8+
49
2.17.2
510
------
611

7-
- Fix scenairo lines containing an ``@`` being parsed as a tag. (The-Compiler)
12+
- Fix scenario lines containing an ``@`` being parsed as a tag. (The-Compiler)
813

914
2.17.1
1015
------

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,13 @@ To have an output in json format:
11261126
py.test --cucumberjson=<path to json report>
11271127

11281128

1129+
To enable gherkin-formatted output on terminal, use
1130+
1131+
::
1132+
1133+
py.test --gherkin-terminal-reporter
1134+
1135+
11291136
Test code generation helpers
11301137
----------------------------
11311138

pytest_bdd/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
from pytest_bdd.steps import given, when, then
44
from pytest_bdd.scenario import scenario, scenarios
55

6-
__version__ = '2.17.2'
6+
__version__ = '2.18.0'
77

88
__all__ = [given.__name__, when.__name__, then.__name__, scenario.__name__, scenarios.__name__]
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
from _pytest.terminal import TerminalReporter
4+
5+
6+
def add_options(parser):
7+
group = parser.getgroup("terminal reporting", "reporting", after="general")
8+
group._addoption(
9+
'--gherkin-terminal-reporter',
10+
action="store_true",
11+
dest="gherkin_terminal_reporter",
12+
default=False,
13+
help=(
14+
"enable gherkin output"
15+
)
16+
)
17+
18+
19+
def configure(config):
20+
if config.option.gherkin_terminal_reporter:
21+
# Get the standard terminal reporter plugin and replace it with our
22+
current_reporter = config.pluginmanager.getplugin('terminalreporter')
23+
if current_reporter.__class__ != TerminalReporter:
24+
raise Exception("gherkin-terminal-reporter is not compatibile with any other terminal reporter."
25+
"You can use only one terminal reporter."
26+
"Currently '{0}' is used."
27+
"Please decide to use one by deactivating {0} or gherkin-terminal-reporter."
28+
.format(current_reporter.__class__))
29+
gherkin_reporter = GherkinTerminalReporter(config)
30+
config.pluginmanager.unregister(current_reporter)
31+
config.pluginmanager.register(gherkin_reporter, 'terminalreporter')
32+
if config.pluginmanager.getplugin("dsession"):
33+
raise Exception("gherkin-terminal-reporter is not compatible with 'xdist' plugin.")
34+
35+
36+
class GherkinTerminalReporter(TerminalReporter):
37+
38+
def __init__(self, config):
39+
TerminalReporter.__init__(self, config)
40+
41+
def pytest_runtest_logstart(self, nodeid, location):
42+
# Prevent locationline from being printed since we already
43+
# show the module_name & in verbose mode the test name.
44+
pass
45+
46+
def pytest_runtest_logreport(self, report):
47+
rep = report
48+
res = self.config.hook.pytest_report_teststatus(report=rep)
49+
cat, letter, word = res
50+
51+
if not letter and not word:
52+
# probably passed setup/teardown
53+
return
54+
55+
if isinstance(word, tuple):
56+
word, word_markup = word
57+
else:
58+
if rep.passed:
59+
word_markup = {'green': True}
60+
elif rep.failed:
61+
word_markup = {'red': True}
62+
elif rep.skipped:
63+
word_markup = {'yellow': True}
64+
feature_markup = {'blue': True}
65+
scenario_markup = word_markup
66+
67+
if self.verbosity <= 0:
68+
return TerminalReporter.pytest_runtest_logreport(self, rep)
69+
elif self.verbosity == 1:
70+
if hasattr(report, 'scenario'):
71+
self.ensure_newline()
72+
self._tw.write('Feature: ', **feature_markup)
73+
self._tw.write(report.scenario['feature']['name'], **feature_markup)
74+
self._tw.write('\n')
75+
self._tw.write(' Scenario: ', **scenario_markup)
76+
self._tw.write(report.scenario['name'], **scenario_markup)
77+
self._tw.write(' ')
78+
self._tw.write(word, **word_markup)
79+
self._tw.write('\n')
80+
else:
81+
return TerminalReporter.pytest_runtest_logreport(self, rep)
82+
elif self.verbosity > 1:
83+
if hasattr(report, 'scenario'):
84+
self.ensure_newline()
85+
self._tw.write('Feature: ', **feature_markup)
86+
self._tw.write(report.scenario['feature']['name'], **feature_markup)
87+
self._tw.write('\n')
88+
self._tw.write(' Scenario: ', **scenario_markup)
89+
self._tw.write(report.scenario['name'], **scenario_markup)
90+
self._tw.write('\n')
91+
for step in report.scenario['steps']:
92+
self._tw.write(' {} {}\n'.format(step['keyword'],
93+
step['name']), **scenario_markup)
94+
self._tw.write(' ' + word, **word_markup)
95+
self._tw.write('\n\n')
96+
else:
97+
return TerminalReporter.pytest_runtest_logreport(self, rep)
98+
self.stats.setdefault(cat, []).append(rep)

pytest_bdd/plugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from . import cucumber_json
77
from . import generation
88
from . import reporting
9+
from . import gherkin_terminal_reporter
910

1011
from .fixtures import *
1112

@@ -33,12 +34,14 @@ def pytest_addoption(parser):
3334
"""Add pytest-bdd options."""
3435
cucumber_json.add_options(parser)
3536
generation.add_options(parser)
37+
gherkin_terminal_reporter.add_options(parser)
3638

3739

3840
@pytest.mark.trylast
3941
def pytest_configure(config):
4042
"""Configure all subplugins."""
4143
cucumber_json.configure(config)
44+
gherkin_terminal_reporter.configure(config)
4245

4346

4447
def pytest_unconfigure(config):
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Feature: Gherkin terminal reporter
2+
3+
Scenario: Should default output be the same as regular terminal reporter
4+
Given there is gherkin scenario implemented
5+
When tests are run
6+
Then output must be formatted the same way as regular one
7+
8+
Scenario: Should verbose mode enable displaying feature and scenario names rather than test names in a single line
9+
Given there is gherkin scenario implemented
10+
When tests are run with verbose mode
11+
Then output should contain single line feature description
12+
And output should contain single line scenario description
13+
14+
Scenario: Should verbose mode preserve displaying of regular tests as usual
15+
Given there is non-gherkin scenario implemented
16+
When tests are run with verbose mode
17+
Then output must be formatted the same way as regular one
18+
19+
Scenario: Should double verbose mode enable displaying of full gherkin scenario description
20+
Given there is gherkin scenario implemented
21+
When tests are run with very verbose mode
22+
Then output must contain full gherkin scenario description
23+
24+
Scenario: Should error message be displayed when no scenario is found
25+
Given there is gherkin scenario without implementation
26+
When tests are run with any verbosity mode
27+
Then output contains error about missing scenario implementation
28+
29+
Scenario: Should error message be displayed when no step is found
30+
Given there is gherkin scenario partially implemented
31+
When tests are run with any verbosity mode
32+
Then output contains error about missing step implementation
33+
34+
Scenario: Should error message be displayed when error occurs during test execution
35+
Given there is gherkin scenario with broken implementation
36+
When tests are run with any verbosity mode
37+
Then output contains error about missing scenario implementation
38+
39+
Scenario: Should local variables be displayed when --showlocals option is used
40+
Given there is gherkin scenario with broken implementation
41+
When tests are run with --showlocals
42+
Then error traceback contains local variable descriptions

0 commit comments

Comments
 (0)