Report a step placed before the first scenario or background (#779)
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_errorpasses again.test_keywords_used_outside_stepsstill passes (no false positive).- Verified locally on gherkin-official 30.1.3 and 40.0.0.
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
:x: Patch coverage is 80.00000% with 6 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 95.90%. Comparing base (9454b38) to head (7571af3).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/pytest_bdd/gherkin_parser.py | 75.00% | 3 Missing and 3 partials :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #813 +/- ##
==========================================
- Coverage 96.10% 95.90% -0.20%
==========================================
Files 55 55
Lines 2390 2420 +30
Branches 136 142 +6
==========================================
+ Hits 2297 2321 +24
- Misses 56 59 +3
- Partials 37 40 +3
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.