2

In Python's behave library, I can write a feature file with a parametrised Scenario Outline like so (adapted from this tutorial):

# feature file
Feature: Scenario Outline Example

  Scenario Outline: Use Blender with <thing>
    Given I put "<thing>" in a blender
    When I switch the blender on
    Then it should transform into "<other thing>"

    Examples: Amphibians
        | thing         | other thing |
        | Red Tree Frog | mush        |
        | apples        | apple juice |

The according step definitions would look like this:

# steps file
from behave   import given, when, then
from hamcrest import assert_that, equal_to
from blender  import Blender

@given('I put "{thing}" in a blender')
def step_given_put_thing_into_blender(context, thing):
    context.blender = Blender()
    context.blender.add(thing)

@when('I switch the blender on')
def step_when_switch_blender_on(context):
    context.blender.switch_on()

@then('it should transform into "{other_thing}"')
def step_then_should_transform_into(context, other_thing):
    assert_that(context.blender.result, equal_to(other_thing))

As one can see, the way to pass the parameters from the feature file into the step functions is

  • by explicitly mentioning them in the feature file enclosed in angle brackets
  • then include the same words enclosed in curly brackets (why not angle brackets again?!) in the decorator of the step function
  • and finally insert these words as function arguments of the step function.

However, given a larger example table with a lot of columns, this quickly gets annoying to write and read:

# feature file
Feature: Large Table Example

  Scenario Outline: Use Blender with a lot of things
    Given I put "<first_thing>", "<second_thing>", "<third_thing>", "<fourth_thing>", "<fifth_thing>", "<sixth_thing>", "<seventh_thing>" in a blender
    When I switch the blender on
    Then it should transform into "<other thing>"

    Examples: Things
        | first thing | second thing | third thing | fourth thing | fifth thing | sixth thing | seventh thing |
        | a     | b             | c  | d           | e            | f           | g           | h             |
        | i     | j             | k  | l           | m            | n           | o           | p             |

# steps file    
@given('I put "{first_thing}", "{second_thing}", "{third_thing}", "{fourth_thing}", "{fifth_thing}", "{sixth_thing}", "{seventh_thing}",  in a blender')
def step_given_put_thing_into_blender(context, first_thing, second_thing, third_thing, fourth_thing, fifth_thing, sixth_thing, seventh_thing):
    context.blender = Blender()
    context.blender.add(thing)
...

I think the point is clear. Is there any possibility to transfer the examples from a large table into the step definition without having to mention all of them explicitly? Are they, for instance, saved somewhere in the context variable even without mentioning them in the text (could not find them there yet)?

4

2 回答 2

1

除了考虑是否真的需要使用大型场景大纲表(参见另一个答案)之外,确实可以访问当前表行而无需明确提及给定/何时/然后步骤中的所有参数context.active_outline (这有点隐藏在文档的附录中)

context.active_outline返回一个可以通过以下方式访问的行为.model.row对象:

  • context.active_outline.headings返回表头列表,无论当前迭代的行是什么(问题示例中的first_thingsecond_thing等)
  • context.active_outline.cells返回当前迭代行的单元格值列表(问题示例中的abc等)
  • 基于索引的访问,例如context.active_outline[0]从第一列返回单元格值(无论标题如何)等。
  • 基于命名的访问,例如返回具有first_thingcontext.active_outline['first_thing']标题的列的单元格值,无论其索引如何

作为context.active_outline.headingscontext.active_outline.cells返回列表,人们也可以做一些有用的事情,比如for heading, cell in zip(context.active_outline.headings, context.active_outline.cells)迭代标题-值对等。

于 2016-08-01T08:36:51.390 回答
0

重点很清楚。

问问自己为什么你真的需要所有这些变量?

您是否试图在一个场景大纲中做太多事情?是否可以将您的问题一分为二?

如果您只实现其中一个,即跳过大纲,场景会是什么样子?会不会又大又笨重?

不幸的是,我不认为你的问题是有很多参数需要提及。您的问题是您尝试使用太多参数。从我坐的地方看,你好像做得太多了。对不起。

于 2016-07-29T17:38:04.670 回答