pytest-bdd icon indicating copy to clipboard operation
pytest-bdd copied to clipboard

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

Open golikovichev opened this issue 5 months ago • 0 comments

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.

golikovichev avatar Jun 19 '26 22:06 golikovichev