使用jsonschema和robotframework验证json

使用jsonschema和robotframework验证json,json,robotframework,jsonschema,python-jsonschema,Json,Robotframework,Jsonschema,Python Jsonschema,有人能帮助我开始学习如何使用RobotFramework通过json模式验证json响应吗 理想情况下,json模式通过http请求进行外部引用:示例 迄今取得的进展: pip install robotframework pip install robotframework-jsonvalidator pip install robotframework-jsonschemalibrary robot .\mytest.robot 我在子目录schemas中有一个名为service.json

有人能帮助我开始学习如何使用RobotFramework通过json模式验证json响应吗

理想情况下,json模式通过http请求进行外部引用:示例

迄今取得的进展:

pip install robotframework pip install robotframework-jsonvalidator pip install robotframework-jsonschemalibrary robot .\mytest.robot 我在子目录
schemas
中有一个名为
service.json

当我运行测试时,我得到

$ robot .\mytest.robot ============================================================================== Mytest ============================================================================== My Test Case: | FAIL | No keyword with name 'Validate Json' found. ------------------------------------------------------------------------------ Mytest | FAIL | 1 critical test, 0 passed, 1 failed 1 test total, 0 passed, 1 failed ============================================================================== Output: E:\GitLab\customer-api\test\output.xml Log: E:\GitLab\customer-api\test\log.html Report: E:\GitLab\customer-api\test\report.html 现在。。。如何使用外部引用的架构文件?任务还在继续


:)

我不确定这是否适用于您正在使用的库,但我正在使用库
jsonschema
()

我提出了两种从文件中使用模式的方法。我会选择第一个

第一条路 在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}
*** 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}
第二条路 在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}
*** 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}
如果要从外部源获取架构文件,请查看请求库。比如:

*** Settings ***
Library     RequestsLibrary

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

您确定JSONSchemaLibrary有一个名为“验证json”的关键字吗?JSONSchemaLibrary是一个真正的机器人框架关键字库,还是仅仅是一个python模块?@BryanOakley-TBH,我对机器人框架和python完全是新手,所以我的答案是,我不知道!我做了一些谷歌搜索,但结果令人失望地稀少。此时,我很高兴收到一条“json模式验证失败”的错误消息——至少我知道我已经正确地设置了模块/库。我的测试基于:尝试导入
JsonValidator
注释-它有一个方法
\u validate\u json
,这可能会导致与您试图从另一个库调用的方法/关键字
validate\u json
冲突。不知道这会不会解决问题,在检查libs源代码后尝试幸运的一击。@todor-谢谢。请查看我对OP的更新。虽然您没有直接提供答案,但它确实让我通过另一条途径找到了答案。:)案例解决了,这是最重要的部分:)顺便说一句,如果我是你,我会删除问题的第一部分(这是一个简单的语法输入错误),因此不会稀释它,并允许任何熟悉库的人专注于剩下的问题。