如何使用需要大量输入并具有大量输出的功能编写小黄瓜测试?

时间:2018-02-12 01:00:18

标签: automated-tests bdd gherkin

我对Gherkin很陌生并且正在努力完成我的第一个项目。基本上我们有很多输入参数,这些参数是花哨计算器所需要的,可以帮助人们弄清楚他们是否可以负担抵押贷款。

我最好专注于输出的一部分,只指定输入所需的输入,或将每个输入列为单独的Given / And? 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%

OR

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 $450,000 as their purchase price
    And the user entered $100,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 $1000 monthly expences
    And...
    And...
    When the user clicks Calculate
    Then the calculator will display the loan amount of $350,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...

第一个让WAY更有意义,更容易阅读,并测试该功能的一个特定部分,所以它接缝就像一个简单的。但是我如何编写并覆盖所有必需的(但对于此测试并不重要)信息填写“并填写了其余的计算器字段”?

我错过了一些明显的东西吗?

2 个答案:

答案 0 :(得分:2)

不幸的是,我被迫不同意先前的回答。使用小黄瓜的最佳方法是举例。我们应该是明确的,而不是隐性的。有一些关于该主题的奇妙书籍,包括“示例规范”和“撰写出色规范”,比我在这里能更好地解释它。这是我写它们的方式:

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 |

注意示例中的内容

  1. 晴天方案
  2. 边界条件
  3. 错误条件

您甚至可以拥有多组示例。您可能有像上面这样的一组示例,这些示例着重于接受标准的最低要求,即您需要非常繁忙的产品所有者批准的最低要求。然后,在单独的示例集中包含用于全面测试的其他示例。很多可能性。

要回答第二个问题-您是否始终在示例表中包含 all 个数据字段?答案是不。您包括重要的数据字段。必须包括实际影响答案的数据字段。多余的数据应删除。您也可以汇总-例如,如果贷款申请人的位置很重要,则可以不考虑完整的地址,而只考虑使用邮政编码。您仍然可以在代码中包含完整的地址。

如果所有数据都很重要,而且非常冗长,那么我将执行以下操作:将测试用例细分为几类。对于每个类别,只有少数几个数据字段会更改,其余的将保持不变。将恒定的变量放在方案上方的Background部分中。如果您仔细地命名类别,即使对于非常复杂/大型的数据字段集,也可以很容易阅读和维护。

此网站很好地记录了小黄瓜:http://docs.behat.org/en/v2.5/guides/1.gherkin.html

答案 1 :(得分:1)

我会做什么:

在这种情况下,我会在后端设置一些虚拟测试数据,以便能够正确测试,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%"
   }
}

如果你还没有技术背景,你甚至可以将它们放在一张桌子里。

注意事项

你在这里测试的确非常重要。如果您正在测试该特定组合(因为它是边缘情况),那么您可能希望保持冗长,但如果您只是检查计算器是否有效,请使用较短的计算器。另请注意,根据您的方案,如果任何其他数据可能导致不同的结果:即一个受抚养人导致更高的贷款额度一个申请人导致贷款金额减少< / em>,那么你可能想要从你的场景中完全删除数据,这就是我在我的例子中所做的。

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

您可以编写一个以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);

如果您需要开发人员知道具体方案是什么,请向他们提供您正在使用的测试数据,或向他们展示测试。