【问题标题】:Validate JSON response with JSON Schema in ROBOT Framework在 ROBOT 框架中使用 JSON Schema 验证 JSON 响应
【发布时间】:2021-11-21 03:13:07
【问题描述】:

我有一个 JSON 响应,它返回公司中用户的所有详细信息。我已将这些响应转换为 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 模式中的类型字符串进行比较时给出通过消息

【问题讨论】:

  • 到目前为止您尝试了什么?你的尝试和错误是什么?
  • 我已经导入了 JSONLibrary,然后尝试从文件加载 json,它给了我错误 No keyword with name load json from file。
  • 我实际上是 JSON 模式方法的新手,如果它是用于单个响应我可以使用的应该是相等的键盘
  • 这个答案展示了如何加载 JSON:stackoverflow.com/questions/24734629/…
  • @Pekka 我已在问题中添加了代码,请查看

标签: api robotframework


【解决方案1】:

我对 requests 库不是很熟悉,但你可以像这样使用 validate:

*** 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}

【讨论】:

    猜你喜欢
    • 2021-04-18
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多