空手道:有没有办法对示例应用函数:变量?

时间:2019-04-04 14:43:35

标签: karate

我的空手道考试通过了:

Feature:  Armstrong numbers

  Background:
    * url baseUrl
    * configure lowerCaseResponseHeaders = true

  Scenario Outline: Find Armstrong numbers in the range from <start> to <end> are <result>
    Given path '/armstrongs'
    And param start = <start>
    And param end = <end>
    When method get
    Then status 200
    And match header content-type contains 'application/json'
    And match header content-type contains 'charset=utf-8'
    And match response == {numbers:<result>, start:<start>, end:<end>, count: <count>, type:Armstrong}
    Examples:
      | start | end    | result                | count
      | 1     | 10     | [1,2,3,4,5,6,7,8,9]   | 9
      | 100   | 1000   | [153, 370, 371, 407]  | 4
      | 90000 | 100000 | [92727, 93084]        | 2

但是,我想做的是没有“示例:”部分中的count变量,而是通过这种方式从变量的长度中得出count:

Feature:  Armstrong numbers

  Background:
    * url baseUrl
    * configure lowerCaseResponseHeaders = true

  Scenario Outline: Find Armstrong numbers in the range from <start> to <end> are <result>
    Given path '/armstrongs'
    And param start = <start>
    And param end = <end>
    When method get
    Then status 200
    And match header content-type contains 'application/json'
    And match header content-type contains 'charset=utf-8'
    And match response == {numbers:<result>, start:<start>, end:<end>, count: <result>.length, type:Armstrong}
    Examples:
      | start | end    | result
      | 1     | 10     | [1,2,3,4,5,6,7,8,9]
      | 100   | 1000   | [153, 370, 371, 407]
      | 90000 | 100000 | [92727, 93084]

尝试此操作时,出现错误:

Armstrong.feature:15 - net.minidev.json.parser.ParseException: Unexpected token . at position 72.

测试失败。

是否可以对示例应用函数:诸如.length之类的变量以获取预期结果并通过测试?

1 个答案:

答案 0 :(得分:0)

进行此更改:

And def result = <result>
And match response == {numbers: '#(result)', start: <start>, end: <end>, count: '#(result.length)', type: 'Armstrong' }