Report a step placed before the first scenario or background (#779)#813
Open
golikovichev wants to merge 2 commits into
Open
Report a step placed before the first scenario or background (#779)#813golikovichev wants to merge 2 commits into
golikovichev wants to merge 2 commits into
Conversation
…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
reviewed
Jun 19, 2026
youtux
left a comment
Contributor
There was a problem hiding this comment.
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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 produceFeatureError: Step definition outside of a Scenario or a Background., so on newer gherkin the malformed feature was accepted silently andtests/parser/test_errors.py::test_step_outside_scenario_or_background_errorstarted 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
FeatureErrorif 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 existingERROR_PATTERNS, it matches the English step keywords.Testing
test_step_outside_scenario_or_background_errorpasses again.test_keywords_used_outside_stepsstill passes (no false positive).