Scenarios 快捷方式
理解为自动绑定,不再不需要 @scenario
from pytest_bdd import scenarios
# assume 'features' subfolder is in this file's directory
scenarios('features')
# assume a feature file
scenarios('steps.feature')
scenarios 的源码
def scenarios(*feature_paths, **kwargs):
"""Parse features from the paths and put all found scenarios in the caller module.
:param *feature_paths: feature file paths to use for scenarios
"""
caller_locals = get_caller_module_locals()
caller_path = get_caller_module_path()
features_base_dir = kwargs.get("features_base_dir")
if features_base_dir is None:
features_base_dir = get_features_base_dir(caller_path)
...
- 可以看到支持传递多个 path
- kwargs 支持传递 features_base_dir 关键字参数
传递多个 path
# 可以是文件夹路径,也可以是具体的某个 feature 文件路径
scenarios('features', 'other_features/some.feature', 'some_other_features')
指定 features_base_dir
会从这个指定的目录下去寻找对应的 feature
三种常见使用
# 指定当前目录
scenarios('steps.feature', 'fixture.feature', features_base_dir=".")
# 相对路径,相对于当前运行该文件的目录
# '/Users/pololuo/work/foris_work_learn/bdd/test/steps.feature'
scenarios('steps.feature', 'fixture.feature', features_base_dir="test")
# 绝对路径
# '/bdd/steps.feature'
scenarios('steps.feature', 'fixture.feature', features_base_dir="/bdd")
联合使用 @scenario 和 scenarios
- @scenario 可以理解为手动绑定某个场景
- scenarios 再自动绑定其他场景 ```python from pytest_bdd import scenario, scenarios
@scenario(‘features/some.feature’, ‘Test something’) def test_something(): pass
assume ‘features’ subfolder is in this file’s directory
scenarios(‘features’) ``` test_something 场景绑定将保持手动,在 features 文件夹中找到的其他场景将自动绑定