Skip to content

Fix empty docstring delivery to step functions (#809)#810

Open
golikovichev wants to merge 1 commit intopytest-dev:masterfrom
golikovichev:fix/809-empty-docstring
Open

Fix empty docstring delivery to step functions (#809)#810
golikovichev wants to merge 1 commit intopytest-dev:masterfrom
golikovichev:fix/809-empty-docstring

Conversation

@golikovichev
Copy link
Copy Markdown

@golikovichev golikovichev commented May 9, 2026

Closes #809.

Problem

When a step has an empty docstring, pytest-bdd raises fixture 'docstring' not found:

When extra labels are injected
  """
  """

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:

docstring=render_string(step.docstring, context) if step.docstring else None,

step.docstring here is a str (set earlier in parse_steps from step.docstring.content). For an empty docstring, that string is "", which is falsy, so the whole expression collapses to None. The downstream check in scenario.py only forwards the docstring to the step function when step.docstring is not None, so the argument is never injected and pytest treats docstring as a missing fixture.

The earlier line in parse_steps is fine because it tests the DocString object itself (always truthy when present) before reading .content.

Fix

Switch the truthy check to is not None so empty docstrings are forwarded as "":

docstring=render_string(step.docstring, context) if step.docstring is not None else None,

Tests

  • New regression test test_steps_with_empty_docstring in tests/steps/test_docstring.py, following the style of the existing tests in that file.
  • Verified the new test fails on master and passes with the fix.
  • Existing docstring tests still pass (pytest tests/steps/test_docstring.py: 5 passed).
  • Targeted run of 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 Unreleased Fixed section.

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.
@golikovichev golikovichev force-pushed the fix/809-empty-docstring branch from 9084928 to 28ac968 Compare May 9, 2026 14:19
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.

Empty docstring results in "fixture not found"

1 participant