Scenarios 快捷方式

理解为自动绑定,不再不需要 @scenario

  1. from pytest_bdd import scenarios
  2. # assume 'features' subfolder is in this file's directory
  3. scenarios('features')
  4. # assume a feature file
  5. scenarios('steps.feature')

scenarios 的源码

  1. def scenarios(*feature_paths, **kwargs):
  2. """Parse features from the paths and put all found scenarios in the caller module.
  3. :param *feature_paths: feature file paths to use for scenarios
  4. """
  5. caller_locals = get_caller_module_locals()
  6. caller_path = get_caller_module_path()
  7. features_base_dir = kwargs.get("features_base_dir")
  8. if features_base_dir is None:
  9. features_base_dir = get_features_base_dir(caller_path)
  10. ...
  • 可以看到支持传递多个 path
  • kwargs 支持传递 features_base_dir 关键字参数

传递多个 path

  1. # 可以是文件夹路径,也可以是具体的某个 feature 文件路径
  2. scenarios('features', 'other_features/some.feature', 'some_other_features')

指定 features_base_dir

会从这个指定的目录下去寻找对应的 feature
三种常见使用

  1. # 指定当前目录
  2. scenarios('steps.feature', 'fixture.feature', features_base_dir=".")
  3. # 相对路径,相对于当前运行该文件的目录
  4. # '/Users/pololuo/work/foris_work_learn/bdd/test/steps.feature'
  5. scenarios('steps.feature', 'fixture.feature', features_base_dir="test")
  6. # 绝对路径
  7. # '/bdd/steps.feature'
  8. 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 文件夹中找到的其他场景将自动绑定