如何编写具有需要大量输入和大量输出的功能的 Gherkin 测试?

How do I write an Gherkin test with a feature that requires a lot of input and has a lot of output?

我对 Gherkin 还很陌生,并且正在为我的第一个项目而苦苦挣扎。基本上我们有很多输入参数,这些参数是花哨的计算器所必需的,可以帮助人们计算出他们是否能负担得起抵押贷款。

我最好只关注输出的一部分并仅指定该输入所需的输入,还是将每个输入单独列出 Given/And?例如

Scenario: Calculate loan amount and LVR
    Given the user is on the purchase calculator page
    And has filled in the rest of the calculator fields
    And entered 0,000 as their purchase price
    And entered 0,000 as their savings
    When the user submits the calculator
    Then the calculator will display the loan amount of 0,000
    And an LVR of 77.78%

Scenario: calculate Homeloan affordability
    Given the user is on the Calculator page
    And the user has entered 21 Fake Street as the purchase property
    And the user entered 0,000 as their purchase price
    And the user entered 0,000 as their savings
    And the user has selected that there are two applicants
    And the user has selected two dependants
    And the user has entered 00 monthly expences
    And...
    And...
    When the user clicks Calculate
    Then the calculator will display the loan amount of 0,000
    And an LVR of 77.78%
    And a predicted interest rate of 4.3%
    And display a google street view image of the property they have selected
    And...
    And...

第一个更有意义,更容易阅读,并且测试了该功能的一个特定部分,因此它看起来很简单。但是我该如何编写它并涵盖所有填写 "And has filled in the rest of the calculator fields" 的必需(但对于此测试不重要)信息?

我是不是遗漏了什么明显的东西?

我会做什么:

在这种情况下,我会在后端设置一些虚拟测试数据,以便能够正确测试,JSON 格式,或标准数据类型(映射或对象)来补充我的场景:

Scenario: Calculating Home Loan Affordability
  Given the user is on the Calculator page
  When the user has entered in their details
  And the user submits the calculator
  Then the user should receive the correct Home Loan information

在我的测试数据中:

{
homeLoanAffordability: {
     addressToBuy: "21 Fake Street",
     purchasePrice: "0,000",
     savings: "0,000"
     applicants: 2,
     dependants: 2,
     ...
     loanAmount: "0,000",
     LVR: "77.78%"
   }
}

如果您没有技术背景,您甚至可以将这些放在 table 中。

注意事项

不过,您在这里测试的内容确实很重要。如果您正在测试该特定组合(因为它是一种边缘情况),那么您可能希望让它冗长,但如果您只是简单地检查计算器是否有效,请使用较短的那个。另请注意,在您的情况下,如果任何其他数据可能导致不同的结果:即 一个受抚养人导致更大的贷款额度一个申请人导致较少的贷款金额,那么您可能希望从您的场景中完全删除数据,这就是我在示例中所做的。

引擎盖下 如果您正在使用基于黄瓜的框架开发测试

您可以编写一个函数来获取 json/standard 数据类型形式的测试数据,并以此作为您进行计算的方式:

function checkHomeLoanAffordability(homeLoanObject){
  // Fill out information
  // Put end information into standard data type
  // return end information
}

其中(至少在 JavaScript 中)可以像这样使用:

let actualHomeLoan = calculateHomeLoan(homeLoanObject),
    testHomeLoan = testData.homeLoanAffordability;

// Using chai.expect
expect(actualHomeLoan.loanAmount).to.equal(testHomeLoan.loanAmount);

如果您需要您的开发人员了解具体场景,请向他们提供您将使用的测试数据或向他们展示幕后测试。

很遗憾,我不得不不同意之前的回复。使用 Gherkin 的最佳方式是通过示例。我们应该是明确的,而不是含蓄的。有一些关于这个主题的很棒的书,包括 "Specification by Example" 和 "Writing Great Specifications",它们比我在这里能解释得更好。以下是我将如何编写它们:

Scenario: Calculate loan amount and Loan to Value Ratio
   Given the user has filled in the purchase calculator information
      | Purchase Price | Savings  | 
      | 0,00        | 0,000 |
   When the user submits the calculator
   Then the calculator will display the loan details
      | Total Loan | LVR    |
      | 0,000   | 77.78% |

更好的是,您可以使用场景大纲

Scenario Outline: Calculate loan amount and Loan to Value Ratio
   Given the user has filled in the <purchase-price> and <savings> calculator information
   When the user submits the calculator
   Then the calculator will display the <total-loan> and <lvr>
   And the message bar will display <message>

Examples:
      | Purchase Price | Savings  | Total Loan | LVR    | Message              |
      | 0,000       | 0,000 | 0,000   | 77.78% |  OK                  |
      | 0,000       | [=11=]       | 0,000   | 100%   |  OK                  |
      | 0,000       | ,000  | ,000    | 50%    |  OK                  |
      | 0,000       | ,999  |  [=11=]        | 0%     | Insufficient savings |

注意示例中包含的内容

  1. 晴天场景
  2. 边界条件
  3. 错误条件

您甚至可以有多组示例。你可以有一组像上面这样的例子,它侧重于接受标准的最低限度——你需要从非常忙碌的产品所有者那里获得批准的最低限度。然后,您在单独的示例集中包含额外示例以进行全面测试。很多可能性。

回答您的第二个问题——在您的示例 table 中,您总是包含 所有 数据字段吗?答案是不。您包括重要的数据字段。必须包括实际影响答案的数据字段。应删除额外的数据。您还可以总结——例如,如果贷款申请人的位置很重要,而不是包括完整地址,请考虑只使用邮政编码。您仍然可以在代码中拥有完整地址。

如果数据很重要,而且很长,那么我会这样做:将测试用例细分为类别。对于每个类别,只有少数数据字段会发生变化,其余的将保持不变。将那些不变的放在场景上方的 Background 部分中。如果您仔细命名类别,即使对于非常 complex/large 组数据字段,这也可以是非常可读和可维护的。

这个站点很好地记录了 Gherkin:http://docs.behat.org/en/v2.5/guides/1.gherkin.html