Conditional Skip¶
Skip steps based on environment, OS, or missing dependencies — without
writing manual if/else guards in every step.
Decorators¶
behave-kit provides four skip decorators that raise unittest.SkipTest
when the condition is not met, causing Behave to mark the step as skipped
rather than failed.
skip_if_env¶
Skip a step when the current environment matches a given name:
from behave_kit import skip_if_env
@skip_if_env("production")
@when("I run the staging-only step")
def step(context):
# This step is skipped when context.config.env == "production"
...
You can also skip on multiple environments by stacking decorators:
@skip_if_env("production")
@skip_if_env("ci")
@when("I run the local-only step")
def step(context):
...
skip_on_os¶
Skip a step on specific operating systems:
from behave_kit import skip_on_os
@skip_on_os("windows")
@when("I run the unix-only step")
def step(context):
...
@skip_on_os("linux")
@when("I run the windows-only step")
def step(context):
...
skip_if_missing¶
Skip a step when a Python module is not installed:
from behave_kit import skip_if_missing
@skip_if_missing("selenium")
@when("I use selenium webdriver")
def step(context):
from selenium import webdriver
...
@skip_if_missing("requests")
@when("I make an HTTP request")
def step(context):
import requests
...
skip_if_no_browser¶
Skip a step when no browser is available (checks for Selenium):
from behave_kit import skip_if_no_browser
@skip_if_no_browser
@when("I open the browser")
def step(context):
from selenium import webdriver
context.browser = webdriver.Chrome()
...
API reference¶
- behave_kit.skip.decorators.skip_if_env(env_name: str) Callable[[Callable[[Concatenate[Context, P]], R]], Callable[[Concatenate[Context, P]], R]][source]¶
Skip the step when
context.config.env == env_name.
- behave_kit.skip.decorators.skip_on_os(os_name: str) Callable[[Callable[[Concatenate[Context, P]], R]], Callable[[Concatenate[Context, P]], R]][source]¶
Skip the step when running on
os_name.
- behave_kit.skip.decorators.skip_if_missing(module_name: str) Callable[[Callable[[Concatenate[Context, P]], R]], Callable[[Concatenate[Context, P]], R]][source]¶
Skip the step when
module_namecannot be imported.
- behave_kit.skip.decorators.skip_if_no_browser(func: Callable[[Concatenate[Context, P]], R] | None = None) Callable[[Concatenate[Context, P]], R] | Callable[[Callable[[Concatenate[Context, P]], R]], Callable[[Concatenate[Context, P]], R]][source]¶
Skip the step when Selenium is not installed.
Can be applied directly (
@skip_if_no_browser) or called as a decorator factory (@skip_if_no_browser()).
Conditions¶
The condition functions used by the decorators are also available standalone:
- behave_kit.skip.conditions.is_env(context: Context, env_name: str) bool[source]¶
True if
context.config.env == env_name.
- behave_kit.skip.conditions.is_os(os_name: str) bool[source]¶
True if the current OS matches
os_name(case-insensitive).
Examples¶
Environment-based skip¶
# environment.py
from behave_kit import setup
def before_all(context):
setup(context, env="staging")
# steps/api_steps.py
from behave_kit import skip_if_env
@skip_if_env("production")
@when("I reset the database")
def step(context):
context.db.reset()
@skip_if_env("staging")
@when("I run the production smoke test")
def step(context):
...
Feature-level skip¶
Apply the decorator to every step in a feature by using a shared step function:
from behave_kit import skip_if_env
@skip_if_env("production")
@given("the test database is ready")
def step(context):
context.db = create_test_db()
Conditional steps with @when_if¶
For more flexible conditional execution, use @when_if which runs a step
only when a condition function returns True:
from behave_kit import when_if
@when_if(lambda ctx: ctx.config.env == "staging")
@when("I run the staging-only step")
def step(context):
...
@when_if(lambda ctx: hasattr(ctx, "browser"))
@when("I take a screenshot")
def step(context):
context.browser.save_screenshot("screenshot.png")
API reference¶
- behave_kit.steps.conditional.when_if(condition: Callable[[Context], object]) Callable[[Callable[[Concatenate[Context, P]], R]], Callable[[Concatenate[Context, P]], R]][source]¶
Decorator: execute the step only if
condition(context)is true.When the condition is false, the step is skipped (via unittest.SkipTest) rather than executed or failed.