-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathgherkin_terminal_reporter.py
99 lines (88 loc) · 4.15 KB
/
gherkin_terminal_reporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from _pytest.terminal import TerminalReporter
def add_options(parser):
group = parser.getgroup("terminal reporting", "reporting", after="general")
group._addoption(
"--gherkin-terminal-reporter",
action="store_true",
dest="gherkin_terminal_reporter",
default=False,
help="enable gherkin output",
)
def configure(config):
if config.option.gherkin_terminal_reporter:
# Get the standard terminal reporter plugin and replace it with our
current_reporter = config.pluginmanager.getplugin("terminalreporter")
if current_reporter.__class__ != TerminalReporter:
raise Exception(
"gherkin-terminal-reporter is not compatible with any other terminal reporter."
"You can use only one terminal reporter."
"Currently '{0}' is used."
"Please decide to use one by deactivating {0} or gherkin-terminal-reporter.".format(
current_reporter.__class__
)
)
gherkin_reporter = GherkinTerminalReporter(config)
config.pluginmanager.unregister(current_reporter)
config.pluginmanager.register(gherkin_reporter, "terminalreporter")
if config.pluginmanager.getplugin("dsession"):
raise Exception("gherkin-terminal-reporter is not compatible with 'xdist' plugin.")
class GherkinTerminalReporter(TerminalReporter):
def __init__(self, config):
super().__init__(config)
def _write_tags(self, tags: list, prefix: str = ""):
tags_string = ", ".join(tags)
return prefix + "(" + tags_string + ")"
def pytest_runtest_logreport(self, report):
rep = report
res = self.config.hook.pytest_report_teststatus(report=rep, config=self.config)
cat, letter, word = res
if not letter and not word:
# probably passed setup/teardown
return
if isinstance(word, tuple):
word, word_markup = word
else:
if rep.passed:
word_markup = {"green": True}
elif rep.failed:
word_markup = {"red": True}
elif rep.skipped:
word_markup = {"yellow": True}
feature_markup = {"blue": True}
scenario_markup = word_markup
tag_markup = {"yellow": True}
if self.verbosity <= 0:
return super().pytest_runtest_logreport(rep)
elif self.verbosity == 1:
if hasattr(report, "scenario"):
self.ensure_newline()
self._tw.write("Feature: ", **feature_markup)
self._tw.write(report.scenario["feature"]["name"], **feature_markup)
self._tw.write("\n")
self._tw.write(" Scenario: ", **scenario_markup)
self._tw.write(report.scenario["name"], **scenario_markup)
self._tw.write(" ")
self._tw.write(word, **word_markup)
self._tw.write("\n")
else:
return super().pytest_runtest_logreport(rep)
elif self.verbosity > 1:
if hasattr(report, "scenario"):
self.ensure_newline()
self._tw.write(self._write_tags(report.scenario["feature"]["tags"]), **tag_markup)
self._tw.write("\n")
self._tw.write("Feature: ", **feature_markup)
self._tw.write(report.scenario["feature"]["name"], **feature_markup)
self._tw.write("\n")
self._tw.write(self._write_tags(prefix=" ", tags=report.scenario["tags"]), **tag_markup)
self._tw.write("\n")
self._tw.write(" Scenario: ", **scenario_markup)
self._tw.write(report.scenario["name"], **scenario_markup)
self._tw.write("\n")
for step in report.scenario["steps"]:
self._tw.write(f" {step['keyword']} {step['name']}\n", **scenario_markup)
self._tw.write(" " + word, **word_markup)
self._tw.write("\n\n")
else:
return super().pytest_runtest_logreport(rep)
self.stats.setdefault(cat, []).append(rep)