行为:用动态示例编写场景大纲

2023-12-01

小黄瓜/行为Examples

Gherkin 语法特点使用示例测试自动化:

Feature: Scenario Outline (tutorial04)

  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 |

    Examples: Consumer Electronics
        | thing         | other thing |
        | iPhone        | toxic waste |
        | Galaxy Nexus  | toxic waste |

测试套件将运行四次,每个示例一次,给出类似于以下内容的结果:

enter image description here

我的问题

如何使用机密数据进行测试Examples部分?例如,我想使用用户 ID 或 SSN 号码测试内部 API,而不将数据硬编码在功能文件中。

有没有办法加载Examples从外部源动态获取?

Update:开了一个github问题 on the behave项目。


我想出了另一个解决方案(behave-1.2.6):

我设法通过使用动态创建场景大纲的示例before_feature.

给定一个特征文件(x.feature):

Feature: Verify squared numbers

  Scenario Outline: Verify square for <number>
    Then the <number> squared is <result>

Examples: Static
  | number | result |
  |   1    |    1   | 
  |   2    |    4   |
  |   3    |    9   |
  |   4    |   16   |

  # Use the tag to mark this outline
  @dynamic
  Scenario Outline: Verify square for <number>
    Then the <number> squared is <result>

Examples: Dynamic
  | number | result |
  |   .    |    .   | 

和步骤文件(steps/x.step):

from behave import step

@step('the {number:d} squared is {result:d}')
def step_impl(context, number, result):
    assert number*number == result

诀窍是使用before_feature in environment.py因为它已经将示例表解析为场景大纲,但尚未从大纲生成场景。

import behave
import copy

def before_feature(context, feature):
    features = (s for s in feature.scenarios if type(s) == behave.model.ScenarioOutline and
                'dynamic' in s.tags)
    for s in features:
        for e in s.examples:
            orig = copy.deepcopy(e.table.rows[0])
            e.table.rows = []
            for num in range(1,5):
                n = copy.deepcopy(orig)
                # This relies on knowing that the table has two rows.
                n.cells = ['{}'.format(num), '{}'.format(num*num)]
                e.table.rows.append(n)

这仅适用于标记为的场景大纲@dynamic.

结果是:

behave -k --no-capture
Feature: Verify squared numbers # features/x.feature:1

  Scenario Outline: Verify square for 1 -- @1.1 Static  # features/x.feature:8
    Then the 1 squared is 1                             # features/steps/x.py:3

  Scenario Outline: Verify square for 2 -- @1.2 Static  # features/x.feature:9
    Then the 2 squared is 4                             # features/steps/x.py:3

  Scenario Outline: Verify square for 3 -- @1.3 Static  # features/x.feature:10
    Then the 3 squared is 9                             # features/steps/x.py:3

  Scenario Outline: Verify square for 4 -- @1.4 Static  # features/x.feature:11
    Then the 4 squared is 16                            # features/steps/x.py:3

  @dynamic
  Scenario Outline: Verify square for 1 -- @1.1 Dynamic  # features/x.feature:19
    Then the 1 squared is 1                              # features/steps/x.py:3

  @dynamic
  Scenario Outline: Verify square for 2 -- @1.2 Dynamic  # features/x.feature:19
    Then the 2 squared is 4                              # features/steps/x.py:3

  @dynamic
  Scenario Outline: Verify square for 3 -- @1.3 Dynamic  # features/x.feature:19
    Then the 3 squared is 9                              # features/steps/x.py:3

  @dynamic
  Scenario Outline: Verify square for 4 -- @1.4 Dynamic  # features/x.feature:19
    Then the 4 squared is 16                             # features/steps/x.py:3

1 feature passed, 0 failed, 0 skipped
8 scenarios passed, 0 failed, 0 skipped
8 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.005s

这依赖于具有正确形状的示例表作为最终表,在我的示例中,有两行。我也不热衷于创造新的behave.model.Row对象,我只需从表中复制一个并更新它。为了更加丑陋,如果您使用文件,您可以将文件名放在示例表中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

行为:用动态示例编写场景大纲 的相关文章

随机推荐

  • 在 OS X 10.9 (Mavericks) 上安装 Java

    我已经安装了JDK on Mac OS X v10 8 山狮 当我升级到Mac OS X v10 9 小牛队 然后跑了java version在终端中 它显示 不存在 Java 运行时 请求安装 然后我在 Mac 上手动安装了 JDK 1
  • CurrentUtcDateTime 不存在 - 实体框架和 MySql

    我在 Entity Framework 4 1 和 MySql Connector Net 6 4 3 中遇到规范函数问题 根据 Microsoft 的说法 所有数据库提供程序都可以从 LINQ 生成的 SQL 中理解规范函数并将其转换为本
  • 让 Linq 对象变得“脏”的最干净的方法是什么?

    我有一个 Linq To SQL 对象obj类型的MyClass我已经通过我的数据上下文加载了 现在我想强制该对象保存 即使没有字段实际更改 以便保存操作可以在幕后触发一些触发器 让我的数据上下文认为最简单的方法是什么obj是脏的 所以调用
  • 为什么标准 R 中值函数比简单的 C++ 替代函数慢得多?

    我对中位数进行了以下实现C 并将其用于R via Rcpp Rcpp export double median2 std vector
  • iphone -- 将 MKMapPoint 距离转换为米

    假设我有一个由四个 CLLocationCooperative2D 点组成的正方形 这些点的单位为经纬度 我想找到该正方形的面积 以米为单位 我将 CLLocationCooperative2D 点转换为 MKMapPoints 然后找到
  • 在Python中打印CSV的所有行只能运行一次

    我编写了一个非常简单的程序 该程序应该读取 CSV 并打印所有行两次 然而 当我运行该程序时 它第一次打印了所有行 第二次则没有打印任何内容 Code import csv csvfile csv reader open
  • JQuery:如何克隆自动完成字段?

    我正在使用 J rn Zaefferer 的 jquery 自动完成插件 当我克隆自动完成字段时 我似乎不知道如何使其工作 它几乎可以工作 因为当我输入文本时 克隆的自动完成字段会显示选项 但我无法选择项目 起初我以为这是一个浏览器兼容性问
  • 需要一种方法将特定数据从 Firebase Realtime DB 提取到 Google Sheets

    我正在尝试使用 AppS 脚本将 Firebase Realtime DB 数据传输到 Google Sheets 中 我需要一种方法来从数据库中提取 ID 部门和姓氏字符串 同时从下面的日志中接收这些字符串 我使用 childByAuto
  • Mac OS X 上的 Java System.getProperty("user.dir")

    我的桌面上有一个 Mac OS X 10 4 上的应用程序包 我的应用程序查找一个名为 resources 的文件夹 其中保存要显示的文件 与可运行的 JAR 保存在同一位置 我知道应用程序包中也有一个名为 Resources 的文件夹 抱
  • 通过意图传递可序列化对象

    这是我第一次尝试寻求解决我的问题的方法 所以请尽量友善 通过简单的搜索 我在这个网站上找到了许多我处理的问题的解决方案 但我想这次我没有运气 我发现没有什么适合我的问题 所以我结束了问一个新问题 我试图通过意图将对象从一个活动传递到另一个活
  • 简单的 MVC 设置/设计?

    我只是想确保我在构建简单的 MVC 应用程序方面朝着正确的方向前进 MODEL interface Reactor NSObject NSNumber temperature NSString lastInspection NSNumber
  • 递归 - 嵌套数组求和

    我正在尝试对嵌套数组求和 1 2 3 4 5 不使用循环 但我不明白到目前为止我所拥有的有什么问题 function sumItems array let sum 0 array forEach item gt if Array isArr
  • Youtube API 检索直播游戏标题

    我正在尝试使用 Youtube Api 检索直播的游戏标题 我目前正在使用 视频 列表api 但它似乎没有返回此信息 它确实给了我一些主题 ID 但它们并不代表特定的游戏 这些信息可以通过API获取吗 谢谢 检查此回复 我认为它满足您的所有
  • Rails 不检测文件更改并且 config.reload_classes_only_on_change 使服务器太慢

    首先我不知道这是否是一个错误 到目前为止 我一直在 Railsdevelopment rb 环境配置中使用此命令 以便该框检测我的文件的更改 config reload classes only on change false 但现在 使用
  • 我如何知道我的 Config.xml 文件是否在 Magento 中工作?

    我已经创建了一个示例模块 但我不确定它是否 module etc config xml文件正在加载 我确信仅此而已就有问题 有没有办法可以确定该文件是否已加载 免费模块列表module 由我构建 会告诉您系统中安装了哪些模块 并会告诉您您的
  • MVC3 ModelBinding 到带有索引间隙的回传集合

    我的模型上有一个对象集合 我使用 EditFor 函数在视图中渲染这些对象 并且我有一个 EditorTemplate 负责实际渲染每个对象 Html EditorFor model gt model MyObjects 这已经运行良好一段
  • Python f.read 未读取正确的字节数

    我的代码应该读取 4 个字节 但有时只读取 3 个字节 f open test sgy r f seek 99716 AAA f read 4 BBB f read 4 CCC f read 4 print len AAA print le
  • 如何使用 Scala 使用比较器对 DataFrame 进行排序?

    我想使用我自己的比较器根据列对 DataFrame 进行排序 Spark SQL 可以做到这一点吗 例如 假设我有一个 DataFrame 注册为表 MyTable 其中列 Day 其类型为 string id Day 1 Fri 2 Mo
  • GraphQL 中的输入类型有何意义?

    您能否解释一下为什么如果突变的输入参数是对象它应该是输入类型 我认为简单得多 只需重复使用type无需提供身份证 例如 type Sample id String name String input SampleInput name Str
  • 行为:用动态示例编写场景大纲

    小黄瓜 行为Examples Gherkin 语法特点使用示例测试自动化 Feature Scenario Outline tutorial04 Scenario Outline Use Blender with