【问题标题】:Validating json with jsonschema and robotframework使用 jsonschema 和 robotsframework 验证 json
【发布时间】:2018-11-18 17:30:56
【问题描述】:

谁能帮助我开始了解如何使用 RobotFramework 通过 json-schema 验证 json 响应?

理想情况下,json-schema 通过 http 请求从外部引用:示例 http://api-bl-uk.northeurope.cloudapp.azure.com/api/v1/crm/schemas/contact

目前的进展:

点安装机器人框架 点安装机器人框架-jsonvalidator pip install robotsframework-jsonschemalibrary 机器人 .\mytest.robot

mytest.robot 在哪里:

库 JsonValidator 库 JSONSchemaLibrary 模式 *** 测试用例 *** 我的测试用例: 验证 Json service.schema.json {"foo": "bar"}

我在子目录schemas 中有一个架构,名为service.json

当我运行测试时,我得到...

$ 机器人 .\mytest.robot ==================================================== ============================== 我的测试 ==================================================== ============================== 我的测试用例:|失败 | 未找到名称为“验证 Json”的关键字。 -------------------------------------------------- ---------------------------- 我的测试 |失败 | 1 次关键测试,0 次通过,1 次失败 共 1 次测试,0 次通过,1 次失败 ==================================================== ============================== 输出:E:\GitLab\customer-api\test\output.xml 日志:E:\GitLab\customer-api\test\log.html 报告:E:\GitLab\customer-api\test\report.html

所以看来我错过了一个相当基本的难题:

找不到名为“Validate Json”的关键字

更新

盲目跟从'示例代码'的问题

问题是我在Library 语句之前缺少*** Settings *** 标头,而且要使用的架构名称错误(在标头修复后很容易解决)。

完整示例:

*** 设置 *** 库 JSONSchemaLibrary 模式 *** 测试用例 *** 我的测试用例: 验证 Json service.json {"foo": "bar"}

现在...如何使用外部引用的架构文件?任务继续!

:)

【问题讨论】:

  • 您确定 JSONSchemaLibrary 有一个名为“validate json”的关键字吗? JSONSchemaLibrary 是一个真正的机器人框架关键字库,还是只是一个 python 模块?
  • @BryanOakley - TBH,我是机器人框架和 python 的新手,所以我的回答是,我不知道!我做了一些谷歌搜索,但结果令人失望地稀疏。此时,我很高兴收到“json 模式验证失败”错误消息 - 至少那时我知道我已经正确设置了模块/库。我的测试基于:github.com/jstaffans/robotframework-jsonschemalibrary
  • 尝试导入 JsonValidator 评论 - 它有一个方法 _validate_json,这可能会导致与您尝试从其他库调用的方法/关键字 validate_json 发生冲突。不知道会不会修复它,在检查 libs 源后尝试幸运的一击。
  • @todor - 谢谢。请参阅我对 OP 的更新。尽管您没有直接提供答案,但它确实将我引向了另一条路线。 :)
  • 案例解决了,这是重要的部分 :) 顺便说一句,如果我是你,我会删除问题的第一部分(这是一个简单的语法错字),因此不会稀释它,并允许任何人图书馆知识渊博的人专注于剩下的问题。

标签: json robotframework jsonschema python-jsonschema


【解决方案1】:

我不确定这是否适用于您正在使用的库,但我正在使用库 jsonschema (https://python-jsonschema.readthedocs.io/)。

我想出了两种方法来使用文件中的架构。我会选择第一个。

第一种方式

在您的 virtualenv 中,运行 pip install jsonschema

然后在与您的测试用例文件相同的目录中创建一个新文件mySchema.json。测试用例文件:

*** Settings ***
# For the "Get Binary File" task
Library     OperatingSystem
# For the "validate" task
Library    jsonschema


*** Test Cases ***
Load json schema from file, and validate json
    # Load the file as a string, usually sufficent for most methods, but not validate() below
    ${schema}    Get Binary File    ./mySchema.json
    # Load the string as a binary object, you could then use this like ${schema}[someProperty] if you wanted to
    ${schema}    evaluate    json.loads('''${schema}''')    json
    # Do a simple validation, using the schema, and your json data. Remember ${instance} needs to be a json object, not just some string
    ${instance}    evaluate    json.loads('''{"someField":[1,2,3]}''')    json
    validate    instance=${instance}    schema=${schema}

第二种方式

在您的 virtualenv 中,运行 pip install jsonschema

然后在与测试用例文件相同的目录中创建一个新文件mySchema.json。测试用例文件:

*** Settings ***
# For the "Get Binary File" task
Library     OperatingSystem
# For the "validate" task
Library    jsonschema


*** Test Cases ***
Load json schema from file, and validate
    # Create a schema
    ${schema}    concat
    ... {
    ...   "type": "object",
    ...   "properties": {"$ref": "file:/absolute/path/to/mySchema.json"}
    ... }
    ${schema}    evaluate    json.loads('''${schema}''')    json
    # Do a simple validation, using the schema, and your json data. Remember ${instance} needs to be a json object, not just some string
    ${instance}    evaluate    json.loads('''{"someField":[1,2,3]}''')    json
    validate    instance=${instance}    schema=${schema}

如果您想从外部来源获取架构文件,请查看 requests 库。比如:

*** Settings ***
Library     RequestsLibrary

*** Test Cases ***
Test case
    Create Session    yourSession    http://localhost
    ${file}    Get Request    yourSession    /filename

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多