/RESTinstance

Robot Framework test library for (RESTful) JSON APIs

Primary LanguagePythonApache License 2.0Apache-2.0

RESTinstance

Robot Framework test library for (RESTful) JSON APIs

Advantages

  1. RESTinstance relies on Robot Framework's language-agnostic, clean and minimal syntax, for API tests. It is neither tied to any particular programming language nor development framework. Using RESTinstance requires little, if any, programming knowledge. It builts on long-term technologies with well established communities, such as HTTP, JSON (Schema), Swagger/OpenAPI and Robot Framework.
  2. It validates JSON using JSON Schema, guiding you to write API tests to base on properties rather than on specific values (e.g. "email must be valid" vs "email is foo@bar.com"). This approach reduces test maintenance when the values responded by the API are prone to change. Although values are not required, you can still test them whenever they make sense (e.g. GET response body from one endpoint, then POST some of its values to another endpoint and verify the results).
  3. It generates JSON Schema for requests and responses automatically, and the schema gets more accurate by your tests. Output the schema to a file and reuse it as expectations to test the other methods, as most of them respond similarly with only minor differences. Or extend the schema further to a full Swagger spec (version 2.0, OpenAPI 3.0 also planned), which RESTinstance can test requests and responses against. All this leads to reusability, getting great test coverage with minimum number of keystrokes and very clean tests.

Installation

Three installation methods are supported. Pick the one that suits you best.

As a Python package

On 3.x and 2.7, you can install or upgrade from PyPi:

pip install --upgrade RESTinstance

As a Docker image

The RESTinstance Docker image contains Python 3.6 and the latest Robot Framework:

docker pull asyrjasalo/restinstance
docker run --rm -ti --env HOST_UID=$(id -u) --env HOST_GID=$(id -g) \
  --env HTTP_PROXY --env HTTPS_PROXY --network host \
  --volume "$PWD/tests":/home/robot/tests \
  --volume "$PWD/results":/home/robot/results \
  asyrjasalo/restinstance tests

Using rfdocker

If already using rfdocker, add RESTinstance to requirements.txt and uncomment the lines in Dockerfile. It is installed next time ./rfdocker is ran.

To pass the proxy settings to the container and run it on the host network:

RUN_ARGS="--env HTTP_PROXY,HTTPS_PROXY --network=host" ./rfdocker

Usage

There is a step-by-step tutorial in the making, best accompanied with the keyword documentation.

Quick start

Tip: Run this README.rst as a test suite with Robot Framework.

*** Settings ***
Library         REST    https://jsonplaceholder.typicode.com
Documentation   Test data can be read from variables and files.
...             Both JSON and Python type systems are supported for inputs.
...             Every request creates a so-called instance. Can be `Output`.
...             Most keywords are effective only for the last instance.
...             Initial schemas are autogenerated for request and response.
...             You can make them more detailed by using assertion keywords.
...             The assertion keywords correspond to the JSON types.
...             They take in either path to the property or a JSONPath query.
...             Using (enum) values in tests optional. Only type is required.
...             All the JSON Schema validation keywords are also supported.
...             Thus, there is no need to write any own validation logic.
...             Not a long path from schemas to full Swagger/OpenAPI specs.
...             The persistence of the created instances is the test suite.
...             Use keyword `Rest instances` to output the created instances.


*** Variables ***
${json}         { "id": 11, "name": "Gil Alexander" }
&{dict}         name=Julie Langford


*** Test Cases ***
GET an existing user, notice how the schema gets more accurate
    GET         /users/1                  # this creates a new instance
    Output      schema response body
    Object      response body             # values are fully optional
    Integer     response body id          1
    String      response body name        Leanne Graham
    [Teardown]  Output                    # note the updated response schema

GET existing users, use JSONPath for very short but powerful queries
    GET         /users?_limit=5           # further assertions are to this
    Array       response body
    Integer     $[0].id                   1           # first id is 1
    String      $[0]..lat                 -37.3159    # any matching child
    Integer     $..id                     maximum=5   # multiple matches
    [Teardown]  Output  $[*].email        # outputs all emails as an array

POST with valid params to create a new user, can be output to a file
    POST        /users                    ${json}
    Integer     response status           201
    [Teardown]  Output  response body     ${OUTPUTDIR}/new_user.demo.json

PUT with valid params to update the existing user, values matter here
    PUT         /users/2                  { "isCoding": true }
    Boolean     response body isCoding    true
    PUT         /users/2                  { "sleep": null }
    Null        response body sleep
    PUT         /users/2                  { "pockets": "", "money": 0.02 }
    String      response body pockets     ${EMPTY}
    Number      response body money       0.02
    Missing     response body moving      # fails if property moving exists

PATCH with valid params, reusing response properties as a new payload
    &{res}=     GET   /users/3
    String      $.name                    Clementine Bauch
    PATCH       /users/4                  { "name": "${res.body['name']}" }
    String      $.name                    Clementine Bauch
    PATCH       /users/5                  ${dict}
    String      $.name                    ${dict.name}

DELETE the existing successfully, save the history of all requests
    DELETE      /users/6                  # status can be any of the below
    Integer     response status           200    202     204
    Rest instances  ${OUTPUTDIR}/all.demo.json  # all the instances so far

Development

Bug reports and feature requests are tracked in GitHub.

We do respect pull request(er)s. Please mention if you do not want to be listed below as contributors.

Library's own tests

For simplicity, Docker is required for running the library's own tests. No other requirements are needed.

To spin up the environment and run the tests:

scripts/test

To run them on Python 2.7:

BUILD_ARGS="-f Dockerfile.python2" BUILD_NAME="restinstance-python2" scripts/test

System under test

The test API is implemented by mounterest, which in turn bases on mountebank.

In the scope of library's tests, mounterest acts as a HTTP proxy to Typicode's live JSON server and uses mountebank's injections to enrich responses slightly, so that they better match to this library's testing needs. Particularly, it allows to test the library with non-safe HTTP methods (POST, PUT, PATCH, DELETE) by mimicking their changes, instead of trying to issue them on the live server. The changes are cleared between the test runs.

Releasing

To update keyword documentation:

scripts/genlibdoc

To create a new .venv, build a Python package and upload it PyPi:

scripts/release_pypi

To build a Docker image with /src, tag it and push it to Docker registry:

scripts/release_docker https://your.private.registry.com/restinstance

To do the same for Docker Hub you can use:

scripts/release_docker {{organization}}/restinstance

Credits

RESTinstance is licensed under Apache License 2.0 and was originally written by Anssi Syrjäsalo.

It was presented at (the first) RoboCon 2018.

Contributors:

We use the following Python excellence under the hood:

  • Flex, by Piper Merriam, for Swagger 2.0 validation
  • GenSON, by Jon "wolverdude" Wolverton, for JSON Schema generator
  • jsonpath-ng, by Tomas Aparicio and Kenneth Knowles, for handling JSONPath queries
  • jsonschema, by Julian Berman, for JSON Schema validator
  • pygments, by Georg Brandl et al., for JSON syntax coloring, in terminal Output
  • requests, by Kenneth Reitz et al., for making HTTP requests

See requirements.txt for all the direct dependencies.