Automated tests 我如何使用需要大量输入和输出的特性编写小黄瓜测试?

Automated tests 我如何使用需要大量输入和输出的特性编写小黄瓜测试?,automated-tests,bdd,gherkin,Automated Tests,Bdd,Gherkin,我对小黄瓜很陌生,正在为我的第一个项目而挣扎。基本上,我们有很多输入参数,这些参数是一个奇特的计算器所必需的,用来帮助人们判断他们是否能够负担得起抵押贷款 我最好只关注输出的一部分,只指定该输入所需的输入,还是将每个输入作为单独的给定/和列出?e、 g Scenario: Calculate loan amount and LVR Given the user is on the purchase calculator page And has filled in the res

我对小黄瓜很陌生,正在为我的第一个项目而挣扎。基本上,我们有很多输入参数,这些参数是一个奇特的计算器所必需的,用来帮助人们判断他们是否能够负担得起抵押贷款

我最好只关注输出的一部分,只指定该输入所需的输入,还是将每个输入作为单独的给定/和列出?e、 g

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 $450,000 as their purchase price
    And entered $100,000 as their savings
    When the user submits the calculator
    Then the calculator will display the loan amount of $350,000
    And an LVR of 77.78%

第一种方法更有意义,更易于阅读,并且测试了功能的一个特定部分,所以它就像一个无脑的人。但是,我如何编写它并涵盖填写“并已填写其余计算器字段”的所有必需(但对本测试不重要)信息


我是不是错过了一些显而易见的事情?

我会怎么做:

在这个场景中,我将在后端设置一些虚拟测试数据,以便能够正确地测试它、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: "$450,000",
     savings: "$100,000"
     applicants: 2,
     dependants: 2,
     ...
     loanAmount: "$350,000",
     LVR: "77.78%"
   }
}
如果你没有技术背景,你甚至可以把它们放在一张表格里

需要注意的事项

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

如果您正在使用基于cucumber的框架开发测试,请在引擎盖下进行

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

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);

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

不幸的是,我不得不不同意前面的回答。使用小黄瓜最好的方法就是以身作则。我们应该是明确的,而不是含蓄的。关于这一主题,有一些很棒的书,包括《示例规范》和《编写伟大的规范》,这些书比我在这里能解释得更好。以下是我将如何编写它们:

Scenario: Calculate loan amount and Loan to Value Ratio
   Given the user has filled in the purchase calculator information
      | Purchase Price | Savings  | 
      | $450,00        | $100,000 |
   When the user submits the calculator
   Then the calculator will display the loan details
      | Total Loan | LVR    |
      | $350,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              |
      | $450,000       | $100,000 | $350,000   | 77.78% |  OK                  |
      | $500,000       | $0       | $500,000   | 100%   |  OK                  |
      | $100,000       | $50,000  | $50,000    | 50%    |  OK                  |
      | $100,000       | $49,999  |  $0        | 0%     | Insufficient savings |
场景概述:计算贷款金额和贷款价值比
给定用户已填写的和计算器信息
当用户提交计算器时
然后计算器将显示和
此时将显示消息栏
示例:
|购买价格|储蓄|总贷款| LVR |信息|
|45万美元10万美元35万美元77.78%可以|
|$500000 |$0 |$500000 | 100%|好的|
|100000美元| 50000美元| 50000美元| 50%|好的|
|100000美元| 49999美元| 0 | 0%|储蓄不足|
请注意该示例如何包括

  • 晴天情景
  • 边界条件
  • 错误条件
  • 您甚至可以有多组示例。您可以有一组像上面这样的示例,它们关注验收标准的最低要求——您需要从非常繁忙的产品负责人处获得批准的最低要求。然后,在一个单独的示例集中包含用于彻底测试的额外示例。很多可能性

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

    如果数据都很重要,而且非常冗长,那么我会做以下工作:将测试用例分为几个类别。对于每个类别,只有少数数据字段将发生更改,其余字段将保持不变。将常量放在场景上方的
    背景
    部分。如果仔细命名类别,即使对于非常复杂/大型的数据字段集,也可以非常可读和可维护

    本网站对小黄瓜的记录非常好:

    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              |
          | $450,000       | $100,000 | $350,000   | 77.78% |  OK                  |
          | $500,000       | $0       | $500,000   | 100%   |  OK                  |
          | $100,000       | $50,000  | $50,000    | 50%    |  OK                  |
          | $100,000       | $49,999  |  $0        | 0%     | Insufficient savings |