Fix empty docstring delivery to step functions (#809)#810
Conversation
ScenarioTemplate.steps_from_template_steps used a truthy check (`if step.docstring`) when building the step list. An empty docstring is a valid value, but `""` is falsy, so the docstring was replaced with None. The step then looked like it had no docstring at all, and pytest fell back to fixture lookup, raising "fixture 'docstring' not found". Switched the check to `is not None` so empty docstrings are passed through as `""`. Added a regression test in tests/steps/test_docstring.py.
9084928 to
28ac968
Compare
|
Hi, just checking if there's anything I can clarify on this. |
|
Friendly nudge - this is still ready whenever someone has a moment. It fixes #809 (empty docstrings were not delivered to step functions), the change is small with a regression test, and CI is green. Happy to rebase onto main if that helps. |
|
Thanks for the review. Quick note on the red CI: those 4 failing tests already fail on master, so they are not from this change. The same set is red on the latest master run (the 2026-06-09 push, run 27186482739):
This branch does not add any new failures. I can open a separate PR to fix the master breakage if that would help. |
|
I dug into the one that reproduces everywhere, test_step_outside_scenario_or_background_error. It is a behavior change in gherkin-official, not a regression in pytest-bdd. On gherkin-official 39.1.0, parsing a feature with a step outside a scenario no longer raises CompositeParserException - the orphan step is silently dropped from the AST. I confirmed it directly: Because no exception is raised, the ERROR_PATTERNS match in gherkin_parser.py never fires, so the expected FeatureError is never raised and the test fails. Two ways to fix, depending on what you prefer:
Happy to send a PR for whichever direction you want. The other CI failures look pin/env-specific - they pass locally on gherkin 39, so they are likely a separate locked-deps issue rather than the same cause. |
Closes #809.
Problem
When a step has an empty docstring, pytest-bdd raises
fixture 'docstring' not found:The reproducer from the issue triggers this on
pytest-bdd 8.1.0.Cause
In
ScenarioTemplate.steps_from_template_steps(src/pytest_bdd/parser.py), the docstring of each rendered step was assigned with a truthy check:step.docstringhere is astr(set earlier inparse_stepsfromstep.docstring.content). For an empty docstring, that string is"", which is falsy, so the whole expression collapses toNone. The downstream check inscenario.pyonly forwards the docstring to the step function whenstep.docstring is not None, so the argument is never injected and pytest treatsdocstringas a missing fixture.The earlier line in
parse_stepsis fine because it tests theDocStringobject itself (always truthy when present) before reading.content.Fix
Switch the truthy check to
is not Noneso empty docstrings are forwarded as"":Tests
test_steps_with_empty_docstringintests/steps/test_docstring.py, following the style of the existing tests in that file.masterand passes with the fix.pytest tests/steps/test_docstring.py: 5 passed).tests/steps,tests/parser/test_parser.py,tests/feature/test_scenario.py,tests/feature/test_outline.py: 42 passed.CHANGES.rst entry added under the
UnreleasedFixed section.