https://www.pythonhosted.org/behave/api.html#environment-file-functions
1. 框架结构
You may optionally include some environmental controls (code to run before and after steps, scenarios, features or the whole shooting match).
The minimum requirement for a features directory is:
features/
features/everything.feature
features/steps/
features/steps/steps.py
A more complex directory might look like:
features/
features/signup.feature
features/login.feature
features/account_details.feature
features/environment.py
features/steps/
features/steps/website.py
features/steps/utils.py
2.
before_step(context, step), after_step(context, step)
These run before and after every step. The step passed in is an instance of Step.
before_scenario(context, scenario), after_scenario(context, scenario)
These run before and after each scenario is run. The scenario passed in is an instance of Scenario.before_feature(context, feature), after_feature(context, feature)These run before and after each feature file is exercised. The feature passed in is an instance of Feature.
before_tag(context, tag), after_tag(context, tag)T
hese run before and after a section tagged with the given name. They are invoked for each tag encountered in the order they’re found in the feature file. See Controlling Things With Tags. The tag passed in is an instance of Tag and because it’s a subclass of string you can do simple tests like:
# -- ASSUMING: tags @browser.chrome or @browser.any are used.
if tag.startswith("browser."):
browser_type = tag.replace("browser.", "", 1)
if browser_type == "chrome":
context.browser = webdriver.Chrome()
else:
context.browser = webdriver.PlainVanilla()
before_all(context), after_all(context)
These run before and after the whole shooting match.
3. 代码调用顺序
before_all
for feature in all_features:
before_feature
for scenario in feature.scenarios:
before_scenario
for step in scenario.steps:
before_step
step.run()
after_step
after_scenario
after_feature
after_all