Skip to content

Add scenarios_class: return a test class instead of injecting into the caller module (#545)#814

Draft
golikovichev wants to merge 1 commit into
pytest-dev:masterfrom
golikovichev:feature/545-scenarios-class
Draft

Add scenarios_class: return a test class instead of injecting into the caller module (#545)#814
golikovichev wants to merge 1 commit into
pytest-dev:masterfrom
golikovichev:feature/545-scenarios-class

Conversation

@golikovichev

Copy link
Copy Markdown

Draft for discussion #545. Opening it early, as discussed, so you can redirect on the API shape before I build out the deprecation path.

What this does

Adds scenarios_class(*feature_paths, ...), which parses the feature files exactly like scenarios() but returns a class instead of injecting the generated tests into the caller module:

from pytest_bdd import scenarios_class

TestLogin = scenarios_class("login.feature")

The generated tests are now visible to editors and linters, and a single scenario can be overridden by subclassing:

class TestLogin(scenarios_class("login.feature")):
    @staticmethod
    @pytest.mark.xfail(reason="known issue")
    @scenario("login.feature", "Failed login")
    def test_failed_login():
        ...

Method names follow the same test_<scenario name> convention (and the same collision suffixing) as scenarios().

Notes and open questions

  • Name. scenarios_class is provisional. Happy to rename to feature_tests or whatever reads best to you.
  • Override needs @staticmethod. The generated wrapper only takes the request and example fixtures, not self, so the methods are stored as staticmethods and an override has to be a staticmethod too (see the second test). If you would rather the override read as a plain method, @scenario would need to become method-aware. Wanted your call before going further.
  • Deprecation path (your steer). Not in this draft. The plan is to emit a DeprecationWarning from scenarios() pointing at the new API, then drop the injection in the next major so it fails loudly. One wrinkle: pytest.ini turns pytest_bdd warnings into errors, so the warning needs the right stacklevel (point at the user module) and the existing suite may need an explicit filter. Happy to do that in a follow-up once the API shape is settled.

Tests

  • scenarios_class returns a collectable class and runs every scenario.
  • subclassing overrides one scenario while the rest run from the base class.

…pytest-dev#545)

scenarios_class parses the feature files like scenarios() but returns a class
whose test_* methods run the scenarios, instead of injecting them into the
caller module. The generated tests are then visible to editors and linters,
and a single scenario can be overridden by subclassing the returned class.
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.

1 participant