【问题标题】:Flexibility of scenarios in Gherkin.Gherkin 中场景的灵活性。
【发布时间】:2016-12-02 21:30:27
【问题描述】:

我正在寻找能够构建更灵活场景的机制。

例如对于测试数据库中记录是否存在的这两个非常相似的场景:

Scenario Outline: Testing query with 1 attribute with these 2 record in and another 2 out of result
  Given I'm connected to <db> database
  When I select <query> from database
  Then Result should contain fields:
    | <row>  |
    | <yes1> |
    | <yes2> |
  And Result should not contain fields:
    | <row> |
    | <no1> |
    | <no2> |

  Examples:
    | db | row   | yes1 | yes2 | no1  | no2  | query                                                            |
    | 1  | model | 1013 | 1006 | 1012 | 1007 | "SELECT model FROM pc WHERE speed >= 3.0;"                       |
    | 1  | maker | E    | A    | C    | H    | "SELECT maker FROM product NATURAL JOIN laptop WHERE hd >= 100;" |

Scenario Outline: Testing query with 2 attributes with these 2 record in and another 2 out of result
  Given I'm connected to <db> database
  When I select <query> from database
  Then Result should contain fields:
    | <rowA>  | <rowB>  |
    | <yes1A> | <yes1B> |
    | <yes2A> | <yes2B> |
  And Result should not contain fields:
    | <rowA> | <rowB> |
    | <no1A> | <no1B> |
    | <no2A> | <no2B> |
  Examples:
    | db | rowA  | rowB    | yes1A  | yes1B | yes2A | yes2B | no1A    | no1B | no2A | no2B | query                              |
    | 1  | model | price   | 1004   | 649   | 2007  | 1429  | 2004    | 1150 | 3007 | 200  | "SELECT model,price FROM product"  |
    | 2  | name  | country | Yamato | Japan | North | USA   | Repulse | Brit | Cal  | USA  | "SELECT name, country FROM clases" |

我希望能够编写一个具有一般属性数量的场景。如果测试的行数也不能确定就好了。

我的梦想是只写一个一般场景

Testing query with N attribute with these M record in and another L out of result

如何在 Gherkin 中做到这一点?是否有可能通过任何黑客攻击?

【问题讨论】:

    标签: oop bdd behat gherkin non-repetitive


    【解决方案1】:

    简短的回答是,不。Gherkin 不是关于灵活性,Gherkin 是关于具体示例。具体的例子是除了灵活之外的一切。

    一个很长的答案是:

    您正在描述 Gherkin 作为测试工具的用法。然而,Gherkin 的目的不是为了测试。 Gherkin 的目的是促进开发人员与需要特定行为的利益相关者之间的沟通。

    如果您想测试某些东西,还有其他工具可以完全支持您想要的东西。任何测试框架都可以使用。我个人的选择是 JUnit,因为我主要使用 Java。

    决定工具的试金石是,谁必须能够理解这一点?

    如果答案是非技术人员,我可能会使用 Gherkin 和非常具体的示例。具体示例很可能不会比较数据库中的内容。具体示例倾向于描述系统的外部可观察行为。

    如果答案是开发人员,那么我可能会使用可以访问编程语言的测试框架。这将满足您所要求的灵活性。

    在您的情况下,您需要一种编程语言。 Gherkin 和 Cucumber 不适合您的情况。

    【讨论】:

      【解决方案2】:

      你可以在没有任何技巧的情况下做到这一点,但我认为你不想这样做,至少不是整个场景都在一行中。
      你会想要遵循 BDD 结构,否则为什么要使用 BDD?

      您应该拥有并遵循如下结构:

      Given
      When
      Then
      

      您需要在初始上下文、操作和结果之间进行拆分并划定界限。在这些之间没有限制将是一种不好的做法。

      另外请注意,清晰的分隔将增加可重用性、可读性,并且还可以帮助您进行调试。

      请研究一下 BDD 的含义以及它的作用,如果您有一份包含 BDD 最佳实践的清单,这也有助于对自动化场景进行代码审查,这可能会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多