首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >大型场景大纲表,而不必显式提及步骤定义中的所有参数。

大型场景大纲表,而不必显式提及步骤定义中的所有参数。
EN

Stack Overflow用户
提问于 2016-07-29 07:52:01
回答 2查看 2.1K关注 0票数 2

在Python的规规矩矩库中,我可以编写一个包含参数化场景大纲的特性文件,如这样(改编自本教程):

代码语言:javascript
复制
# 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 |

相应的步骤定义如下所示:

代码语言:javascript
复制
# 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函数的方法是

  • 通过在包含尖括号的特性文件中显式地提到它们
  • 然后把同样的词括在花括号里(为什么不再加上尖括号呢?!)在阶跃函数的修饰器中
  • 最后,将这些单词作为step函数的函数参数插入。

但是,考虑到一个包含大量列的更大的示例表,编写和读取这样的表会很快变得很烦人:

代码语言:javascript
复制
# 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)
...

我想这点很清楚。是否有可能将示例从一个大型表转移到步骤定义中,而不必显式地提及它们?例如,它们是否保存在上下文变量中,即使没有在文本中提及它们(还没有在那里找到它们)?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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.headingscontext.active_outline.cells返回列表,我们也可以做一些有用的事情,比如for heading, cell in zip(context.active_outline.headings, context.active_outline.cells)来迭代标题-值对等等。

票数 1
EN

Stack Overflow用户

发布于 2016-07-29 17:38:04

重点很清楚。

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

你是不是想在一个场景大纲中做太多的事情?能把你的问题一分为二吗?

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

不幸的是,我不认为您的问题是有许多参数需要提及。您的问题是,您试图使用太多的参数。从我的位置看,你似乎做得太多了。抱歉的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38653356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档