使用jsonschema和robotframework

时间:2018-06-08 15:51:26

标签: json robotframework jsonschema python-jsonschema

任何人都可以帮我开始学习如何使用RobotFramework通过json架构验证json响应吗?

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

到目前为止的进展:

pip install robotframework
pip install robotframework-jsonvalidator
pip install robotframework-jsonschemalibrary
robot .\mytest.robot

mytest.robot的位置:

Library JsonValidator
Library JSONSchemaLibrary schemas
*** Test Cases ***
  My Test Case:
   Validate Json  service.schema.json  {"foo": "bar"}

我在名为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

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

找不到名称为“验证Json”的关键字

更新

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

问题是我在*** Settings ***语句之前缺少Library标题,加上要使用的模式名称错误(在标题修复后很容易解决)。

完整示例:

*** Settings ***
Library  JSONSchemaLibrary  schemas

*** Test Cases ***
My Test Case:
    Validate Json  service.json  {"foo": "bar"}

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

:)

1 个答案:

答案 0 :(得分:0)

我不确定这是否适用于您正在使用的库,但是我正在使用库jsonschemahttps://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}

如果要从外部源获取模式文件,请查看请求库。像这样:

*** Settings ***
Library     RequestsLibrary

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