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.

```python
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:
```python
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:
```python
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.


```python
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:
```python
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:
```python
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:
```python

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

## Transactions

Apiritif allows to group multiple requests or actions into a transaction using a `transaction` context manager.

```python
with transaction("group"):
http.get("https://blazedemo.com/first").assert_ok()
http.get("https://blazedemo.com/second").assert_ok()
```

Transaction defines the name for the test. This will be displayed on the BlazeMeter report.

```
with apiritif.transaction('My Test Name'):
response = target.get(self.url, headers=headers)
response.assert_2xx()
```

## 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.6.tar.gz (17.2 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.6-py2.py3-none-any.whl (20.6 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: apiritif-0.6.6.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for apiritif-0.6.6.tar.gz
Algorithm Hash digest
SHA256 d2f226e447fa2731365d7416c07cc2367a5a9c9cf5e1d04a156ce0b0bd5ea8c9
MD5 bf28f1a08c849276777d1f74b5c622e1
BLAKE2b-256 002f292322c3ce42ceaceb273e1a333d7fcdc5913b7595c6dd63bf6667faa4f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: apiritif-0.6.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for apiritif-0.6.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3dc52e3be4377e92362828f722dd84086ecb1aa08849da0d55af6c9d7f46d635
MD5 2d71cad4d9ed4ea4e7764e162c9d65be
BLAKE2b-256 892449d97dac6c3d38552f6af9645181253bdd133a7db885f4e0a3099a0466f1

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