From 64d2d774c532bbbd25ab6dbf33428e83e9c3729a Mon Sep 17 00:00:00 2001 From: Ryan Casperson Date: Tue, 23 Jul 2019 13:45:49 -0600 Subject: [PATCH] Allow for scenario descriptions to be present fixes #311 This treats scenario descriptions the same way it does feature descriptions. --- pytest_bdd/feature.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pytest_bdd/feature.py b/pytest_bdd/feature.py index e09a4dd3..0e0e9bc7 100644 --- a/pytest_bdd/feature.py +++ b/pytest_bdd/feature.py @@ -60,6 +60,8 @@ STEP_PARAM_RE = re.compile(r"\<(.+?)\>") COMMENT_RE = re.compile(r'(^|(?<=\s))#') +TYPES_WITH_DESCRIPTIONS = [types.FEATURE, types.SCENARIO, types.SCENARIO_OUTLINE] + def get_step_type(line): """Detect step type by the beginning of the line. @@ -291,7 +293,8 @@ def __init__(self, basedir, filename, encoding="utf-8", strict_gherkin=True): multiline_step = False stripped_line = line.strip() clean_line = strip_comments(line) - if not clean_line and (not prev_mode or prev_mode not in types.FEATURE): + if not clean_line and (not prev_mode or prev_mode not in TYPES_WITH_DESCRIPTIONS): + # Blank lines are included in feature and scenario descriptions continue mode = get_step_type(clean_line) or mode @@ -340,6 +343,13 @@ def __init__(self, basedir, filename, encoding="utf-8", strict_gherkin=True): # Remove Feature, Given, When, Then, And keyword, parsed_line = parse_line(clean_line) if mode in [types.SCENARIO, types.SCENARIO_OUTLINE]: + # Lines between the scenario declaration + # and the scenario's first step line + # are considered part of the scenario description. + if scenario and not keyword: + scenario.add_description_line(parsed_line) + continue + tags = get_tags(prev_line) self.scenarios[parsed_line] = scenario = Scenario(self, parsed_line, line_number, tags=tags) elif mode == types.BACKGROUND: @@ -435,6 +445,7 @@ def __init__(self, feature, name, line_number, example_converters=None, tags=Non self.tags = tags or set() self.failed = False self.test_function = None + self._description_lines = [] def add_step(self, step): """Add step to the scenario. @@ -456,6 +467,22 @@ def steps(self): result.extend(self._steps) return result + def add_description_line(self, description_line): + """Add a description line to the scenario. + + :param str description_line: + """ + self._description_lines.append(description_line) + + @property + def description(self): + """Get the scenario's description. + + :return: The scenario description + """ + return u"\n".join(self._description_lines).strip() + + @property def params(self): """Get parameter names.