Hooks and Wiring

The setup() and teardown() functions wire all behave-kit modules into a Behave context with a single call.

setup()

behave_kit.hooks.setup(context: Context, *, env: str | None = None, config_file: str = 'behave.toml', log_level: str = 'INFO', continue_after_failed: bool | None = None) None[source]

Wire all behave-kit modules into context.

Idempotent: calling twice is a no-op. Each module is wired independently in try/except — a failure in one does not prevent the others.

Parameters:
  • context – The Behave context object.

  • env – Optional environment name for profile-based configuration.

  • config_file – Path to the configuration file (default behave.toml).

  • log_level – Logging level for the behave_kit logger.

  • continue_after_failed – When True, scenarios continue executing remaining steps after a failure. When False, the default Behave behaviour (stop on first failure) is restored. None leaves the current setting unchanged.

Wires the following modules (each independently, fault-tolerant):

  • env config — loads behave.toml and attaches KitConfig to context

  • soft asserts — activates SoftAssertCollector via contextvars

  • context dump — enables automatic context dump on scenario failure

  • suggestions — wires “did you mean?” hints for undefined steps

  • fixtures — creates a FixtureManager and attaches it to context

teardown()

behave_kit.hooks.teardown(context: Context) None[source]

Clean up wired modules in reverse order.

Only modules that were successfully wired during setup() are torn down. Safe to call without a prior setup() (no-op). Also tears down any class-based step instances created during the scenario, even if setup() was not called.

Cleans up wired modules in reverse order:

  1. Teardown fixtures (runs each fixture’s teardown function)

  2. Cleanup scoped attributes

  3. Report soft assertion failures (raises if any collected)

  4. Dump context if scenario failed

Examples

Full automatic wiring

from behave_kit import setup, teardown

def before_all(context):
    setup(context, env="staging", config_file="behave.toml")

def before_scenario(context, scenario):
    # Re-activate soft asserts for each scenario
    from behave_kit import use_soft_asserts
    use_soft_asserts(context)
    # Setup fixtures for this scenario
    manager = context._behave_kit_fixtures
    manager.setup_for_scenario(context, scenario)

def after_scenario(context, scenario):
    # Clear soft assert failures before teardown
    collector = getattr(context, "_behave_kit_soft", None)
    if collector is not None:
        collector.clear()
    # Teardown fixtures
    manager = context._behave_kit_fixtures
    manager.teardown_scenario(context)
    # Full teardown
    teardown(context)

Minimal wiring

from behave_kit import setup, teardown

def before_all(context):
    setup(context)

def after_scenario(context, scenario):
    teardown(context)

With log level

from behave_kit import setup

def before_all(context):
    setup(context, env="staging", log_level="DEBUG")

Without env config

from behave_kit import setup

def before_all(context):
    setup(context)  # No env config, but soft asserts + fixtures + dump still wired

Idempotency

from behave_kit import setup

def before_all(context):
    setup(context, env="staging")
    setup(context, env="staging")  # No-op, already wired

Fault tolerance

If one module fails to wire, the others still work:

from behave_kit import setup

def before_all(context):
    # Even if behave.toml is missing, soft asserts and fixtures still work
    setup(context, env="staging", config_file="missing.toml")
    # Logs: WARNING: Failed to wire env config