Item 对象

YamlItem是用例的python对象,拥有以下属性可供钩子函数使用

警告

注意:item内部的核心组件,不要修改item中的内容!

pytest_yaml.YamlItem.name

当用例的用例名(yaml文件中的name字段)

pytest_yaml.YamlItem.nodeid

当用例的id(目录+文件名+用例名),具有为唯一性

pytest_yaml.YamlItem.yaml_data

yaml中的原始内容

pytest_yaml.YamlItem.max_step_no

用例的步骤数量

pytest_yaml.YamlItem.current_step_no

当前的步骤序号,0表示是第一个步骤。和 max_step_no 相等则表示是最后一个步骤

pytest_yaml.YamlItem.is_first_step

当前步骤是否是第一个步骤

pytest_yaml.YamlItem.is_last_step

当前步骤是否是最后一个步骤

pytest_yaml.YamlItem.current_step

当前的步骤内容,是一个字典。

pytest_yaml.YamlItem.usefixtures

当前的用例请求的fixtures及其结果,是一个字典,fixture名为key,返回值为value。

pytest_yaml.YamlItem.add_locals(**kwargs)

向当前用例动态注入变量。注入后,当前用例的后续步骤可以通过 ${name} 使用该变量。

只能在 pytest_yaml_run_step hook 中调用,变量仅影响当前用例, 不会泄漏到其他并发执行的用例中。

def pytest_yaml_run_step(item):
    step = item.current_step
    if "login" in step:
        item.add_locals(token="abc123")
pytest_yaml.YamlItem.add_globals(**kwargs)

向全局模板上下文添加变量或函数。与 pytest_yaml.add_globals() 等效, 但通过 item 调用,在 hook 中无需额外导入。