如何在空手道特定 json 模式中使用模板

How to use templating in karate specific json schema

我正在尝试下面的空手道。

我在 .json 文件中有一个 json 模式(用于响应验证)。在许多模式中常见的 REGEX 很少。我想将它们作为键值对提取到一个公共文件中,并在其他模式中使用它。可能吗?如果是这样,我该怎么做? json 架构中允许使用模板吗?

示例:

示例Json 架构文件(示例-response.json):

{
  "response": {
    "name": "#string",
    "amount": "#regex ^(-?)([0]|[1-9][0-9]{0,15})[.][0-9]{2}$"
  }
}

专题文件

Feature: Example feature

  Background:

  * def sampleResponse = read('./sample-response.json');



  Scenario: Example scenario

    When url 'https://someurl.com'
    And method get
    Then status 200
    And  match response == sampleResponse

我想做什么?

我想将数量正则表达式存储在 json 文件中作为可重用变量,并在 json 文件中使用模板来替换它。 可能吗?


{
  "response": {
    "name": "#string",
    "amount": "{{get regex from this template}}"
  }
}

是的。 Embedded expressions work even when reading files.

所以这样做:

{
  "response": {
    "name": "#string",
    "amount": "#(amount)"
  }
}

然后这样做:

Background:
* def amount = 100
* def sampleResponse = read('sample-response.json')

如果你想让amount来自另一个JSON文件,为什么不呢,下面说的是data.json:

{ "amount": 100 }

然后你这样做:

Background:
* def data = read('data.json')
# you don't need the next line if you use "data.amount" as the embedded expression
* def amount = data.amount
* def sampleResponse = read('sample-response.json')