Skip to main content

Robot Framework library for RESTful JSON APIs

Project description

RESTinstance (1.4.4)

Robot Framework library for RESTful JSON APIs

Keyword Documentation

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

Install and upgrade from PyPi:

pip install --upgrade RESTinstance

Usage

See the keyword documentation.

Quick start

  1. Create two new empty directories, atest and results.
  2. Create a new file atest/YOURNAME.robot with the content:
*** 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 schema             # 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
  1. Make JSON API testing great again:
robot --outputdir results atest/

Contributing

Kindly create an issue and then create a pull request. See CONTRIBUTING.md for that.

Install PDM:

python -m pip install --user pdm

Install dependencies:

pdm install

Acceptance tests require you starting mountebank testapi/ in another terminal first:

pdm testenv

To run all tests:

pdm tests

You can debug mountebank requests and responses created by acceptance tests at localhost:2525.

Arguments can be passed to pytest and robot, respectively:

pdm test -- test/<test_modulename>.py
pdm atest -- atest/<atest_suitefile>.robot

Credits

RESTinstance is under LGPL-3.0 license and is originally written by Anssi Syrjäsalo.

It was first presented at the first RoboCon, 2018.

Contributors:

  • Eficode Ltd.

We use 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
  • requests, by Kenneth Reitz et al., for making HTTP requests

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

restinstance-1.4.4.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

RESTinstance-1.4.4-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file restinstance-1.4.4.tar.gz.

File metadata

  • Download URL: restinstance-1.4.4.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.17.1 CPython/3.11.9 Darwin/23.5.0

File hashes

Hashes for restinstance-1.4.4.tar.gz
Algorithm Hash digest
SHA256 a43f3e204cee186bbf2c74d2e009c6f56c361209817073bf5ad7cfaf6ef88c8a
MD5 5187b9ee994b4c57beaaa76f476bd54b
BLAKE2b-256 eec20c096c987b5bb720337fc0668084aa21c64b647e5a6b13acdb6cd851ad9e

See more details on using hashes here.

File details

Details for the file RESTinstance-1.4.4-py3-none-any.whl.

File metadata

  • Download URL: RESTinstance-1.4.4-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.17.1 CPython/3.11.9 Darwin/23.5.0

File hashes

Hashes for RESTinstance-1.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bbbc7a05efdcf10be4258072212befa83cc625eb544d5a2c2518208da3340206
MD5 bd7c6ed7f944e4f43ad9c37c0ff0a1a0
BLAKE2b-256 dc6e91b469ae898d4cfda85ed8f4f4a305edd6850285d961028dfe603764a783

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page