Skip to content

feat: use scenarios() shortcut in code generation template#796

Open
toroleapinc wants to merge 3 commits intopytest-dev:masterfrom
toroleapinc:feat/issue-535-use-scenarios-shortcut
Open

feat: use scenarios() shortcut in code generation template#796
toroleapinc wants to merge 3 commits intopytest-dev:masterfrom
toroleapinc:feat/issue-535-use-scenarios-shortcut

Conversation

@toroleapinc
Copy link
Copy Markdown

Summary

Update the code generation template to use the scenarios() shortcut instead of individual @scenario() decorators.

Closes #535

Changes

Modified src/pytest_bdd/templates/test.py.mak to:

  • Import scenarios instead of scenario
  • Generate scenarios('feature_file.feature') calls instead of individual @scenario(...) decorated test functions
  • Automatically deduplicate feature file paths

Before

from pytest_bdd import given, scenario, then, when

@scenario('features/login.feature', 'Successful login')
def test_successful_login(): ...

@scenario('features/login.feature', 'Failed login')
def test_failed_login(): ...

After

from pytest_bdd import given, scenarios, then, when

scenarios('features/login.feature')

Replace individual @Scenario() decorators with the scenarios()
shortcut function in the code generation template. This produces
cleaner, more concise generated test code.

Before:
    @Scenario('feature.feature', 'Scenario 1')
    def test_scenario_1(): ...

    @Scenario('feature.feature', 'Scenario 2')
    def test_scenario_2(): ...

After:
    scenarios('feature.feature')

Closes pytest-dev#535

Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com>
Comment thread src/pytest_bdd/templates/test.py.mak Outdated
@@ -1,22 +1,22 @@
<%
feature_paths = sorted(set(s.feature.rel_filename for s in scenarios))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Shouldn't we use the features car instead of scenarios?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, updated to use features directly.

Comment on lines +1 to +3
<%
feature_paths = sorted(set(f.rel_filename for f in features))
%>\
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should go right before line 14

youtux
youtux previously approved these changes Mar 12, 2026
The template now emits a single scenarios('foo.feature') call instead
of one @Scenario(...) decorator per scenario, so the expected output
strings in test_generate, test_generate_with_quotes, and
test_unicode_characters need to match.
@toroleapinc
Copy link
Copy Markdown
Author

Pushed ed581b5 — updated the expected output strings in tests/scripts/test_generate.py (test_generate, test_generate_with_quotes, test_unicode_characters) to match the new scenarios() codegen output. Verified locally on Python 3.13: the 3 codegen tests pass, and the only remaining test failure I see (test_step_outside_scenario_or_background_error) reproduces on upstream/master too, so it's pre-existing and not from this PR.

Looks like the CI run is currently blocked on action_required (workflow approval for the fork). Could a maintainer approve it when you get a chance? Thanks.

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.

Scenario generation should use scenarios shortcut

2 participants