在Python的规规矩矩库中,我可以编写一个包含参数化场景大纲的特性文件,如这样(改编自本教程):
# 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 |相应的步骤定义如下所示:
# 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))可以看到,将参数从特性文件传递到step函数的方法是
但是,考虑到一个包含大量列的更大的示例表,编写和读取这样的表会很快变得很烦人:
# 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)
...我想这点很清楚。是否有可能将示例从一个大型表转移到步骤定义中,而不必显式地提及它们?例如,它们是否保存在上下文变量中,即使没有在文本中提及它们(还没有在那里找到它们)?
发布于 2016-08-01 08:36:51
除了考虑是否确实需要使用大型场景大纲表(请参阅另一个答案)之外,确实可以访问当前的表行,而无需通过context.active_outline (文档附录中隐藏了一点)显式地提及给定/when/ row中的所有参数。
context.active_outline返回可以以下列方式访问的behave.model.row对象:
context.active_outline.headings返回表标题的列表,不管当前迭代的行是什么(问题中的示例中有first_thing、second_thing等)。context.active_outline.cells返回当前迭代行的单元格值列表(问题中的示例中的a、b、c等)。context.active_outline[0],从第一列返回单元格值(不管标题)等等。context.active_outline['first_thing'] )将返回带有first_thing头的列的单元格值,而不管其索引如何。作为context.active_outline.headings和context.active_outline.cells返回列表,我们也可以做一些有用的事情,比如for heading, cell in zip(context.active_outline.headings, context.active_outline.cells)来迭代标题-值对等等。
发布于 2016-07-29 17:38:04
重点很清楚。
问问自己,为什么你真的需要所有这些变量?
你是不是想在一个场景大纲中做太多的事情?能把你的问题一分为二吗?
如果您只是实现其中之一,即跳过大纲,那么场景会是什么样的呢?会不会又大又笨重呢?
不幸的是,我不认为您的问题是有许多参数需要提及。您的问题是,您试图使用太多的参数。从我的位置看,你似乎做得太多了。抱歉的。
https://stackoverflow.com/questions/38653356
复制相似问题