Report a step placed before the first scenario or background (#779)#813
Report a step placed before the first scenario or background (#779)#813golikovichev wants to merge 5 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
left a comment
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.
|
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. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #813 +/- ##
==========================================
+ Coverage 96.12% 96.19% +0.07%
==========================================
Files 55 55
Lines 2423 2471 +48
Branches 136 142 +6
==========================================
+ Hits 2329 2377 +48
Misses 57 57
Partials 37 37 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…e-scenario # Conflicts: # CHANGES.rst
…#779) Add tests for the description keyword helpers so the new detection code is fully exercised: an unknown language yields no keywords, a step keyword later in a description is allowed, a missing feature is a no-op, and the location fallback fires when the offending line is not found verbatim.
|
Merged main to resolve the conflict, and added tests covering the remaining branches of the description check: an unknown language yields no keywords, a keyword later in a description is allowed, a missing feature is a no-op, and the location fallback fires when the offending line is not found verbatim. CI should be green now. The switch to the Gherkin dialect keywords is still in place, so the detection follows Gherkin's own step logic. Happy to adjust anything. |
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).