在 ROBOT Framework 中使用 JSON 架构验证 JSON 响应

Validate JSON response with JSON Schema in ROBOT Framework

我有一个 JSON 回复,其中 returns 公司用户的所有详细信息。我已将这些响应转换为 JSON 模式并将其保存在文件名 JSONSchema.json.

现在如何将所有响应与 JSON 模式进行比较?有人可以帮忙吗

JSON数据

[  {
    "Id": 2,
    "Name": "Test123",
    "Status": "Active"
  },
  {
    "Id": 3,
    "Name": "123Test",
    "Status": "Active"
    }
    ]

JSON 架构

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "Id": {
          "type": "integer"
        },
        "Name": {
          "type": "string"
        },
        "Status": {
          "type": "string"
        }
      },
      "required": [
        "Id",
        "Name",
        "Status"
      ]
    },
    {
      "type": "object",
      "properties": {
        "Id": {
          "type": "integer"
        },
        "Name": {
          "type": "string"
        },
        "Status": {
          "type": "string"
        }
      },
      "required": [
        "Id",
        "Name",
        "Status"
      ]
    }
  ]
}

代码

*** Settings ***
Library           jsonschema

*** Test Cases ***
${get_value}=  GET On Session  xyz  /details  

${json_response}=     set variable    ${get_value.json()}

${schema}    Get Binary File    ./myschema.json

${schema}    evaluate    json.loads('''${schema}''')    json

${instance}    evaluate    json.loads('''{"Name":[0,1]}''')    json
 validate    instance=${instance}    schema=${schema}

我对代码的最后两行有疑问。

我希望我的 json 响应与模式进行比较,因为当我比较 Name : Test123 与 JSON 模式

中的类型字符串时给出通过消息

我对请求库不是很熟悉,但你可以像这样使用验证:

*** Settings ***
Library               RequestsLibrary
Library               jsonschema
Suite Setup           Init payloads

*** Test Cases ***
Schematest
    ${response}=        Post     url=http://postman-echo.com/post    json=${payload}
    ${response_json}=   Set Variable  ${response.json()['data']}
    Log To Console      ${response_json}
    Evaluate            jsonschema.validate(instance=${response_json}, schema=${schema})

*** Keywords ***
Init payloads
    ${payload_str}=     Catenate   SEPARATOR=
...        [  {
...            "Id": 2,
...            "Name": "Test123",
...            "Status": "Active"
...          },
...          {
...            "Id": 3,
...            "Name": "123Test",
...            "Status": "Active"
...            }
...            ]
    ${payload}=    Evaluate    json.loads('''${payload_str}''')       json
    Set Suite Variable    ${payload}
    ${schema_str}=     Catenate   SEPARATOR=
...       {
...         "$schema": "http://json-schema.org/draft-04/schema#",
...         "type": "array",
...         "items": [
...           {
...             "type": "object",
...             "properties": {
...               "Id": {
...                 "type": "integer"
...               },
...               "Name": {
...                 "type": "string"
...               },
...               "Status": {
...                 "type": "string"
...               }
...             },
...             "required": [
...               "Id",
...               "Name",
...               "Status"
...             ]
...           },
...           {
...             "type": "object",
...             "properties": {
...               "Id": {
...                 "type": "integer"
...               },
...               "Name": {
...                 "type": "string"
...               },
...               "Status": {
...                 "type": "string"
...               }
...             },
...             "required": [
...               "Id",
...               "Name",
...               "Status"
...             ]
...           }
...         ]
...       }
    ${schema}=    Evaluate    json.loads('''${schema_str}''')       json
    Set Suite Variable    ${schema}