python - 检查字段是否存在于 python-behave 的 context.table 中

标签 python gherkin python-behave

我在尝试检查某个字段是否存在于表中时遇到问题:

| category    | info                       |
| Electrician | 74300 Paris 04 50 65 43 21 |

假设我的表通常包含一个“名称”字段,但在这个特定情况下它不包含。在这种情况下,如何检查“名称”字段是否存在?

最佳答案

您可以简单地检查您的列是否出现在表格的 headings 属性中。在处理单行实例时也存在此属性,如您所知。

if 'name' in context.table.headings:

    do_something()

但是,我个人更喜欢将表格作为字典处理,使用类似这样的东西

def make_dict_from_row(row):
    """
    creates a dictionary of arguments (**kwargs) from a behave
    table row.
    """

    cells = [cell if cell != '' else None for cell in row.cells]
    return dict(zip(row.headings, cells))

使用这个你可以简单地做到这一点

if 'name' in row:

    do_something()

参见 table model以供进一步引用。

关于python - 检查字段是否存在于 python-behave 的 context.table 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45879363/

相关文章:

python - KeyError : "' __name_ _' not in globals" occurs on import from local directory

python - 在 Pandas 中进行反向多热编码的最快方法是什么?

java - Cucumber Runner 类未运行步骤定义

pytest - 如何运行 pytest-bdd 测试?

python - 有没有一种简单的方法可以将步骤定义为给定和何时

bdd - 使用场景大纲处理 Gherkin 中的大字符串

python - 向结构化numpy数组添加字段(三)

python - 获取传入函数的名称

python - 内存管理和 Tkinter

ruby - 如何在 Cucumber 的功能名称中使用空格