Reputation: 9816
Can anyone help get me started on how to use RobotFramework to validate json responses via a json-schema?
Ideally, the json-schema is externally referenced via an http request: Example http://api-bl-uk.northeurope.cloudapp.azure.com/api/v1/crm/schemas/contact
Progress so far:
pip install robotframework pip install robotframework-jsonvalidator pip install robotframework-jsonschemalibrary robot .\mytest.robot
Where mytest.robot
is:
Library JsonValidator Library JSONSchemaLibrary schemas *** Test Cases *** My Test Case: Validate Json service.schema.json {"foo": "bar"}
I have a schema in the subdirectory schemas
called service.json
When I run the test I get...
$ 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
So it seems I'm missing a fairly basic piece of the puzzle:
No keyword with name 'Validate Json' found
The problems of blindly following 'sample code'
The problem was I was missing the *** Settings ***
header prior to the Library
statements, plus the name of the schema to use was wrong (easy to solve after the header was fixed).
Full example:
*** Settings *** Library JSONSchemaLibrary schemas *** Test Cases *** My Test Case: Validate Json service.json {"foo": "bar"}
Now... How do I use external referenced schema files? The quest continues!
:)
Upvotes: 2
Views: 8467
Reputation: 1820
I'm not sure if this will work with the library you are using, but I'm using the library jsonschema
(https://python-jsonschema.readthedocs.io/).
There are two ways I came up with for using a schema from a file. I'd go with the first.
In your virtualenv, run pip install jsonschema
.
Then create a new file, mySchema.json
in the same directory as your test case file. Test case file:
*** 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}
In your virtualenv, run pip install jsonschema
.
Then create a new file, mySchema.json
in the same directory as your test case file. Test case file:
*** 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}
If you want to get the schema file from an external source, have a look at the requests library. Something like:
*** Settings ***
Library RequestsLibrary
*** Test Cases ***
Test case
Create Session yourSession http://localhost
${file} Get Request yourSession /filename
Upvotes: 3