Skip to main content

A pseudo-functional API Testing library.

Project description

Assertio

Pseudo-functional Python api testing framework.

Installation

pip install assertio

Basic Usage

Assertio consists on create requests on a chain way, you can organize this requests within a Runner class in order to define test cases.

You can find below an example using the Pokemon API

# main.py
from assertio import Request, Runner


class MyRunner(Runner):

    def test_get_pikachu(self):
        Request()\
            .to("/pokemon/pikachu")\
            .with_method("GET")\
            .perform()\
            .assert_http_ok()\
            .assert_response_field("name")\
            .equals("pikachu")

MyRunner().start()

This will run all the defined methods within MyRunner as long as they start with test.

Wait, where should I add my API URL? Well you can export an enviromnent variable for that!

$ export ASSERTIO_BASE_URL=https://pokeapi.co/api/v2

Once this environment variable is setup let's start the test!

$ python main.py

Should do the trick!

But that's not it!

Assertio has many assertions defined and is flexible enough to let you define your custom assertions and preconditions.

You can chain as many assertions and preconditions as you want, just remember to keep it simple, if your request chain is 15 lines long, maybe it's time to consider to split it in smaller tests.

Anyway, let's take a look to a more complex example using a POST to a custom API.

Remember to change the ASSERTIO_BASE_URL environment variable

$ export ASSERTIO_BASE_URL=http://my-books-api-domain/api/v1

And let's try to add a new resource.

# main.py
from assertio import Request, Runner

DEFAULT_HEADERS = {"Content-Type": "application/json"}
BOOK_PAYLOAD = {
    "id": 144,
    "title": "The Divine Comedy", 
    "author": {
        "id": 12,
        "name": "Dante Alighieri",
        "nationality": "Italian"
    }, 
    "year": 1472
}

class MyRunner(Runner):

    def test_create_book(self):
        Request()\
            .to("/books/")\
            .with_method("POST")\
            .with_headers(DEFAULT_HEADERS)\
            .with_body(BOOK_PAYLOAD)\
            .perform()\
            .assert_http_created()\
            .assert_response_field("author.nationality")\
            .equals("Italian")

MyRunner().start()

All the tests must start with test! Otherwise start() method will not run it

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

assertio-1.4.0b0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

assertio-1.4.0b0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file assertio-1.4.0b0.tar.gz.

File metadata

  • Download URL: assertio-1.4.0b0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for assertio-1.4.0b0.tar.gz
Algorithm Hash digest
SHA256 ae0798e315e1dd1c451f0d8c24922324efc7e0fb4d648a7fd0d958b8c659fd0f
MD5 740613b9ee1692b2e3f001e9de7b2451
BLAKE2b-256 f2dcdbe41ae4a0f655bc6e84cc62cac8eede065471184449456c3ca4c5a4ea4d

See more details on using hashes here.

File details

Details for the file assertio-1.4.0b0-py3-none-any.whl.

File metadata

  • Download URL: assertio-1.4.0b0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for assertio-1.4.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 1323abaa869c3e33452dabecffc449f437f4b4e97866378188fd2c2d4d5ac19a
MD5 184b3a00be938c058ed8b728cc0f3913
BLAKE2b-256 0719144df2280620f886d8c4ec05cc68c5b6eeeab7b6733fa4017c6cc30ac937

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