Skip to main content

Python framework for API testing

Project description

Apiritif

Apiritif is a number of utilities aimed to simplify the process of maintaining API tests.

Feature list:

  • utilities for making HTTP requests
  • assertions on them
  • transactions

Overview

HTTP Requests

Apiritif allows to use simple requests-like API for making HTTP requests.

from apiritif import http

response = http.get("http://example.com")
response.assert_ok()  # will raise AssertionError if request wasn't successful

http object provides the following methods:

from apiritif import http

http.get("http://api.example.com/posts")
http.post("http://api.example.com/posts")
http.put("http://api.example.com/posts/1")
http.patch("http://api.example.com/posts/1")
http.delete("http://api.example.com/posts/1")
http.head("http://api.example.com/posts")

All methods (get, post, put, patch, delete, head) support the following arguments:

def get(address,               # URL for the request
        params=None,           # URL params dict
        headers=None,          # HTTP headers
        cookies=None,          # request cookies
        data=None,             # raw request data
        json=None,             # attach JSON object as request body
        allow_redirects=True,  # automatically follow HTTP redirects
        timeout=30)            # request timeout, by default it's 30 seconds

HTTP Targets

Target is an object that captures resource name of the URL (protocol, domain, port) and allows to set some settings applied to all requests made for a target.

from apiritif import http

qa_env = http.target("http://192.160.0.2")
qa_env.get("/api/v4/user")
qa_env.get("/api/v4/user")

Target constructor supports the following options:

target = apiritif.http.target(
    address,               # target base address
    base_path=None,        # base path prepended to all paths (e.g. '/api/v2')
    use_cookies=True,      # use cookies
    additional_headers=None,  # additional headers for all requests
    keep_alive=True,       # reuse opened HTTP connection
    auto_assert_ok=True,   # automatically invoke 'assert_ok' after each request
)

Assertions

Apiritif responses provide a lot of useful assertions that can be used on responses.

Here's the list of assertions that can be used:

response = http.get("http://example.com/")

# assert that request succeeded (status code is 2xx or 3xx)
response.assert_ok()
# assert that request has failed
response.assert_failed()

# status code based assertions
response.assert_2xx()
response.assert_3xx()
response.assert_4xx()
response.assert_5xx()
response.assert_status_code(code)
response.assert_not_status_code(code)

# content-based assertions

# assert that response body contains a string
response.assert_in_body(member)

# assert that response body doesn't contain a string
response.assert_not_in_body(member)

# search (or match) response body with a regex
response.assert_regex_in_body(regex, match=False)
response.assert_regex_not_in_body(regex, match=False)

# assert that response has header
response.assert_has_header(header)

# assert that response has header with given value
response.assert_header_value(header, value)

# assert that response's headers contains a string
response.assert_in_headers(member)
response.assert_not_in_headers(member)

# search (or match) response body with a regex
response.assert_regex_in_headers(member)
response.assert_regex_not_in_headers(member)

# assert that response body matches JSONPath query
response.assert_jsonpath(jsonpath_query, expected_value=None)
response.assert_not_jsonpath(jsonpath_query)

# assert that response body matches XPath query
response.assert_xpath(xpath_query, parser_type='html', validate=False)
response.assert_not_xpath(xpath_query, parser_type='html', validate=False)

Note that assertions can be chained, so the following construction is entirely valid:

response = http.get("http://example.com/")
response.assert_ok().assert_in_body("Example")

Transactions

TODO: transactions

Taurus Integration

TODO: describe that Taurus can extract Apiritif's action log and handle it.

Logging

TODO: Describe that Apiritif creates 'apiritif' logger that can be used to debug http requests and write test interactively.

TODO: describe how to silence Apiritif logging.

Project details


Download files

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

Source Distribution

apiritif-0.6.3.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apiritif-0.6.3-py2.py3-none-any.whl (17.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file apiritif-0.6.3.tar.gz.

File metadata

  • Download URL: apiritif-0.6.3.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for apiritif-0.6.3.tar.gz
Algorithm Hash digest
SHA256 0610654f8ec1aabb5aa83ef35c36af6f68338303c8a653ea1d15a2360bd608a7
MD5 2729fdf5379e66081321e537d6ab0891
BLAKE2b-256 176849b7f2f803715c8a733321afe36b7f393624089ccd6901961ba840bcee1f

See more details on using hashes here.

File details

Details for the file apiritif-0.6.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for apiritif-0.6.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7c4d63a456a0d2649e9187baeff7b9e2409d7ddbae60895d6aad2c6ad0985d13
MD5 a90bbdda2409448961ae12809249e26a
BLAKE2b-256 b6f9bee205da37e6b2a0dc8bc40ba2d096f8df274a325c6f9293fc446c885a16

See more details on using hashes here.

Supported by

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