pytest-bdd
pytest-bdd copied to clipboard
Add scenarios_class: return a test class instead of injecting into the caller module (#545)
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_classis provisional. Happy to rename tofeature_testsor whatever reads best to you. - Override needs
@staticmethod. The generated wrapper only takes the request and example fixtures, notself, 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,@scenariowould 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
DeprecationWarningfromscenarios()pointing at the new API, then drop the injection in the next major so it fails loudly. One wrinkle:pytest.initurnspytest_bddwarnings 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_classreturns a collectable class and runs every scenario.- subclassing overrides one scenario while the rest run from the base class.