Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/pytest_bdd/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from .exceptions import StepError
from .gherkin_parser import Background as GherkinBackground
from .gherkin_parser import DataTable, GherkinDocument, get_gherkin_document
from .gherkin_parser import Child, DataTable, GherkinDocument, get_gherkin_document
from .gherkin_parser import Feature as GherkinFeature
from .gherkin_parser import Rule as GherkinRule
from .gherkin_parser import Scenario as GherkinScenario
Expand Down Expand Up @@ -492,12 +492,13 @@ def parse(self) -> Feature:
)

for child in feature_data.children:
if child.background:
feature.background = self.parse_background(child.background)
elif child.rule:
self._parse_and_add_rule(child.rule, feature)
elif child.scenario:
self._parse_and_add_scenario(child.scenario, feature)
match child:
case Child(background=GherkinBackground() as background):
feature.background = self.parse_background(background)
case Child(rule=GherkinRule() as rule):
self._parse_and_add_rule(rule, feature)
case Child(scenario=GherkinScenario() as scenario):
self._parse_and_add_scenario(scenario, feature)

return feature

Expand Down
34 changes: 34 additions & 0 deletions tests/parser/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import textwrap
from pathlib import Path

from src.pytest_bdd import parser as parser_module
from src.pytest_bdd.gherkin_parser import (
Background,
Cell,
Expand Down Expand Up @@ -851,3 +853,35 @@ def test_parser():
)

assert gherkin_doc == expected_document


def test_feature_parser_skips_unknown_child_kinds(tmp_path, monkeypatch):
"""A Child with none of background/rule/scenario set is skipped.

The gherkin AST reserves the right to grow new child kinds; ``Child.from_dict``
would map such a child to an all-``None`` dataclass, and ``FeatureParser.parse``
must ignore it instead of crashing.
"""
(tmp_path / "minimal.feature").write_text(
textwrap.dedent(
"""\
Feature: Minimal
Scenario: A scenario
Given a step
"""
)
)

real_get_gherkin_document = parser_module.get_gherkin_document

def get_gherkin_document_with_unknown_child(abs_filename: str, encoding: str = "utf-8") -> GherkinDocument:
document = real_get_gherkin_document(abs_filename, encoding)
document.feature.children.append(Child())
return document

monkeypatch.setattr(parser_module, "get_gherkin_document", get_gherkin_document_with_unknown_child)

feature = parser_module.FeatureParser(str(tmp_path), "minimal.feature").parse()

assert list(feature.scenarios) == ["A scenario"]
assert feature.background is None
Loading