Skip to content

Commit

Permalink
Allow overriding the types labels and make cucumber.json report as we…
Browse files Browse the repository at this point in the history
…ll as gherkin terminal output consistent with the overriden language
  • Loading branch information
Tiago Miranda authored and Tiago Miranda committed Jan 10, 2020
1 parent f3b92bd commit 99d83e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
13 changes: 9 additions & 4 deletions pytest_bdd/cucumber_json.py
Expand Up @@ -7,7 +7,8 @@
import sys
import time

from .feature import force_unicode
from . import feature
from . import types


def add_options(parser):
Expand Down Expand Up @@ -72,7 +73,7 @@ def _get_result(self, step, report, error_message=False):
if report.passed or not step["failed"]: # ignore setup/teardown
result = {"status": "passed"}
elif report.failed and step["failed"]:
result = {"status": "failed", "error_message": force_unicode(report.longrepr) if error_message else ""}
result = {"status": "failed", "error_message": feature.force_unicode(report.longrepr) if error_message else ""}
elif report.skipped:
result = {"status": "skipped"}
result["duration"] = int(math.floor((10 ** 9) * step["duration"])) # nanosec
Expand Down Expand Up @@ -142,9 +143,13 @@ def stepmap(step):
"result": self._get_result(step, report, error_message),
}

feature_label = '{}'.format(feature.prefix_by_type(types.FEATURE)).split(":")[0]
scenario_label = '{}'.format(feature.prefix_by_type(types.SCENARIO)).split(":")[0]


if scenario["feature"]["filename"] not in self.features:
self.features[scenario["feature"]["filename"]] = {
"keyword": "Feature",
"keyword": feature_label,
"uri": scenario["feature"]["rel_filename"],
"name": scenario["feature"]["name"] or scenario["feature"]["rel_filename"],
"id": scenario["feature"]["rel_filename"].lower().replace(" ", "-"),
Expand All @@ -156,7 +161,7 @@ def stepmap(step):

self.features[scenario["feature"]["filename"]]["elements"].append(
{
"keyword": "Scenario",
"keyword": scenario_label,
"id": report.item["name"],
"name": scenario["name"],
"line": scenario["line_number"],
Expand Down
8 changes: 8 additions & 0 deletions pytest_bdd/feature.py
Expand Up @@ -581,3 +581,11 @@ def add_step(self, step):
"""Add step to the background."""
step.background = self
self.steps.append(step)

def prefix_by_type(reqtype):

for prefix, _type in STEP_PREFIXES:
if _type==reqtype:
return prefix

return '<N/A type> {}'.format(reqtype)
17 changes: 11 additions & 6 deletions pytest_bdd/gherkin_terminal_reporter.py
Expand Up @@ -6,7 +6,9 @@

from _pytest.terminal import TerminalReporter

from .feature import STEP_PARAM_RE
from . import feature

from . import types


def add_options(parser):
Expand Down Expand Up @@ -77,15 +79,18 @@ def pytest_runtest_logreport(self, report):
feature_markup = {"blue": True}
scenario_markup = word_markup

feature_label = '{}'.format(feature.prefix_by_type(types.FEATURE))
scenario_label = ' {}'.format(feature.prefix_by_type(types.SCENARIO))

if self.verbosity <= 0:
return TerminalReporter.pytest_runtest_logreport(self, rep)
elif self.verbosity == 1:
if hasattr(report, "scenario"):
self.ensure_newline()
self._tw.write("Feature: ", **feature_markup)
self._tw.write(feature_label, **feature_markup)
self._tw.write(report.scenario["feature"]["name"], **feature_markup)
self._tw.write("\n")
self._tw.write(" Scenario: ", **scenario_markup)
self._tw.write(scenario_label, **scenario_markup)
self._tw.write(report.scenario["name"], **scenario_markup)
self._tw.write(" ")
self._tw.write(word, **word_markup)
Expand All @@ -95,10 +100,10 @@ def pytest_runtest_logreport(self, report):
elif self.verbosity > 1:
if hasattr(report, "scenario"):
self.ensure_newline()
self._tw.write("Feature: ", **feature_markup)
self._tw.write(feature_label, **feature_markup)
self._tw.write(report.scenario["feature"]["name"], **feature_markup)
self._tw.write("\n")
self._tw.write(" Scenario: ", **scenario_markup)
self._tw.write(scenario_label, **scenario_markup)
self._tw.write(report.scenario["name"], **scenario_markup)
self._tw.write("\n")
for step in report.scenario["steps"]:
Expand All @@ -115,7 +120,7 @@ def pytest_runtest_logreport(self, report):

def _format_step_name(self, step_name, **example_kwargs):
while True:
param_match = re.search(STEP_PARAM_RE, step_name)
param_match = re.search(feature.STEP_PARAM_RE, step_name)
if not param_match:
break
param_token = param_match.group(0)
Expand Down

0 comments on commit 99d83e9

Please sign in to comment.