是否可以使用空值的场景大纲表?

时间:2015-03-27 10:31:32

标签: python bdd gherkin

Scenaio大纲:Blah de blah   当我输入并输入输入字段时   然后一切都很好

示例:
  | a | b |
  | 1 | 2 |
  | | 3 |

上述方案在BBD Behave中引发以下错误 测试未定义 请定义测试

我不确定如何解决这个问题。 有什么建议?

3 个答案:

答案 0 :(得分:1)

据我所知,你无法做到。但是,您可以使用空字符串或占位符值(例如,' N / A'),您可以在步骤定义中查找。

答案 1 :(得分:1)

确实可以使用空表格单元格(如在您的示例中,不使用&#34;&#34;或其他东西),如果您可以在给定/何时/然后步骤中没有明确提及您的参数。< / p>

在您的示例中,这意味着您不得像这样编写步骤定义

Given two parameters <a> and <b>
...

@given('two parameters {a} and {b}
def step(context, a, b):
  # test something with a and b

但是喜欢这样:

Given two parameters a and b # <-- note the missing angle brackets
...

@given('two parameters a and b') <-- note the missing curly brackets
def step(context): # <-- note the missing function arguments for a and b
  # ...

现在,为了访问当前的表格行,您可以使用context.active_outline (which is a bit hidden in the appendix of the documentation)

context.active_outline返回一个behave.model.row对象,可以通过以下方式访问该对象:

  • context.active_outline.headings返回表头的列表,无论当前迭代的行是什么( a b 在问题的示例中)< / LI>
  • context.active_outline.cells返回当前迭代行的单元格值列表( 1 2 和&#39;&#39;(空字符串)在问题的示例中可以使用if not...), 3 进行测试)
  • 基于索引的访问,如context.active_outline[0],返回第一列的单元格值(无论标题)等。
  • 基于名称的访问(如context.active_outline['a'])会返回带有 a 标题的列的单元格值,无论其索引是什么

作为context.active_outline.headingscontext.active_outline.cells返回列表,还可以执行有用的内容,例如for heading, cell in zip(context.active_outline.headings, context.active_outline.cells)来迭代标题 - 值对等。

答案 2 :(得分:0)

使用Custom Type Conversions中所述的https://pypi.org/project/parse/

import parse

from behave import given, register_type


@parse.with_pattern(r'.*')
def parse_nullable_string(text):
    return text


register_type(NullableString=parse_nullable_string)

@given('params "{a:NullableString}" and "{b:NullableString}"'
def set_params(context, a, b):
  # a or b will be empty if they are blank in the Examples
  context.a = a
  context.b = b

现在功能文件看起来像这样,

Given params "<a>" and "<b>"
# Rest of the steps
Examples:
 | a | b |
 | 1 | 2 |
 |   | 3 |