"""Per-run and per-scenario state attached to behave's context."""from__future__importannotationsfromtypingimportAny,Protocolfromsteplib.core.registryimportStepRegistry
[docs]classBehaveContext(Protocol):"""Minimal protocol for behave's ``context`` object. Attributes: config: The behave configuration object. steplib: The ``SteplibState`` attached by steplib's autoload/load. """config:Anysteplib:Any
[docs]classSteplibState:"""Holds the steplib state attached to ``context.steplib``. Modules set their own namespaces as attributes on this object (e.g. ``state.api = ApiContext(...)``). Args: context: The behave context object. registry: The ``StepRegistry`` populated during autoload/load. """def__init__(self,context:Any,registry:StepRegistry)->None:"""Initialize state with a behave context and step registry."""self._context=contextself._registry=registry@propertydefcontext(self)->Any:"""The behave context."""returnself._context@propertydefregistry(self)->StepRegistry:"""The step registry."""returnself._registry
[docs]defreset(self)->None:"""Reset per-scenario state. Called from ``before_scenario``. Iterates over all module-level attributes (non-underscore) and calls ``reset()`` if available. """fornameinlist(self.__dict__):ifname.startswith("_"):continueattr=getattr(self,name)ifhasattr(attr,"reset")andcallable(attr.reset):attr.reset()
[docs]defcleanup(self)->None:"""Close resources after a scenario. Called from ``after_scenario``. Iterates over all module-level attributes (non-underscore) and calls ``cleanup()`` if available. """fornameinlist(self.__dict__):ifname.startswith("_"):continueattr=getattr(self,name)ifhasattr(attr,"cleanup")andcallable(attr.cleanup):attr.cleanup()