Skip to content

Report a step placed before the first scenario or background (#779)#813

Open
golikovichev wants to merge 2 commits into
pytest-dev:masterfrom
golikovichev:fix/779-step-outside-scenario
Open

Report a step placed before the first scenario or background (#779)#813
golikovichev wants to merge 2 commits into
pytest-dev:masterfrom
golikovichev:fix/779-step-outside-scenario

Conversation

@golikovichev

Copy link
Copy Markdown

Fixes #779.

Problem

gherkin-official 31 and later no longer raise a parse error when a step keyword (Given/When/Then/And/But) is placed before the first scenario or background. Instead the line is folded into the feature description. pytest-bdd relied on gherkin raising that error (mapped through ERROR_PATTERNS) to produce FeatureError: Step definition outside of a Scenario or a Background., so on newer gherkin the malformed feature was accepted silently and tests/parser/test_errors.py::test_step_outside_scenario_or_background_error started failing.

Fix

After a successful parse, pytest-bdd now checks whether the first non-empty line of the feature description starts with a step keyword and raises the same FeatureError if so. This keeps the behaviour consistent across all supported gherkin-official versions: older versions still report it through the existing parse-error path, newer versions through this post-parse check.

The check only looks at the first non-empty description line, so a legitimate multi-line description that mentions a keyword later is not affected (covered by tests/feature/test_steps.py::test_keywords_used_outside_steps). Like the existing ERROR_PATTERNS, it matches the English step keywords.

Testing

  • test_step_outside_scenario_or_background_error passes again.
  • test_keywords_used_outside_steps still passes (no false positive).
  • Verified locally on gherkin-official 30.1.3 and 40.0.0.

…ytest-dev#779)

gherkin-official 31+ no longer raises a parse error when a step keyword appears before the first scenario or background; it folds the line into the feature description. pytest-bdd now re-detects that case after parsing so a FeatureError is raised consistently across supported gherkin-official versions.

@youtux youtux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rather have the Gherkin lead here and follow the same logic

…st-dev#779)

Address review feedback: instead of a hardcoded English regex, reuse the
Gherkin dialect keywords for the feature's language so a step placed before
the first scenario is reported in any language Gherkin supports. Add a
French regression test.
@golikovichev

Copy link
Copy Markdown
Author

Good call. Switched to the Gherkin dialect keywords for the feature's language instead of the hardcoded English ones, so the check follows Gherkin's own step detection and works for non-English features too. Added a French regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_step_outside_scenario_or_background_error fails with current gherkin-official

2 participants