Skip to content

feat: use scenarios() shortcut in code generation template (take 2)#825

Open
Pierre-Sassoulas wants to merge 3 commits into
masterfrom
scenarios-codegen-take2
Open

feat: use scenarios() shortcut in code generation template (take 2)#825
Pierre-Sassoulas wants to merge 3 commits into
masterfrom
scenarios-codegen-take2

Conversation

@Pierre-Sassoulas

@Pierre-Sassoulas Pierre-Sassoulas commented Jul 5, 2026

Copy link
Copy Markdown
Member

Re-applies #796 (reverted in #824 because it was merged before review comments were addressed), with @youtux's review comments addressed:

  • features vs scenarios var: kept the fix from the original PR — the template iterates over features directly.
  • Template block placement: the <% feature_paths = ... %> computation now sits right before the loop that uses it, inside the % if features: block, instead of at the top of the file (comment).
  • Idempotency (comment): yes, it is preserved, and now covered by a test (test_generate_is_idempotent):
    • invoking the autogen script twice yields byte-identical output (feature_paths is a sorted(set(...)), so deterministic);
    • applying the generated code twice to the same module does not duplicate tests, because scenarios() skips scenarios already bound in the caller module.

Also adds the changelog entry that was missing from the original PR.

Original work by @toroleapinc.

Closes #535

toroleapinc and others added 2 commits July 5, 2026 20:00
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 #535

Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
- Move the feature_paths computation next to the loop that uses it,
  inside the 'if features' block (review comment from youtux).
- Add a test proving generation is idempotent: invoking the script
  twice yields identical output, and applying the generated code
  twice to the same module does not duplicate tests (scenarios()
  skips already bound scenarios).
- Add the changelog entry that was missing from #796.
@codecov

codecov Bot commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.17%. Comparing base (582f045) to head (e20e492).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #825      +/-   ##
==========================================
+ Coverage   96.12%   96.17%   +0.05%     
==========================================
  Files          55       55              
  Lines        2423     2432       +9     
  Branches      136      136              
==========================================
+ Hits         2329     2339      +10     
  Misses         57       57              
+ Partials       37       36       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Pierre-Sassoulas Pierre-Sassoulas marked this pull request as draft July 5, 2026 18:29
@Pierre-Sassoulas Pierre-Sassoulas marked this pull request as ready for review July 5, 2026 18:54
def test_${ make_python_name(scenario.name)}():
${make_python_docstring(scenario.name)}
<%
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 can be inlined

)

first = pytester.run("pytest-bdd", "generate", "idempotency.feature")
second = pytester.run("pytest-bdd", "generate", "idempotency.feature")

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.

Oh I thought this script was writing to a file... if not then this test is irrelevant

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

3 participants