Skip to main content

API automated testing in Python

This is a starting point for automating API's in Python using the pytest and the AIOHTTP or HTTPX libraries on macOS.

Required dependencies

In the terminal run

  1. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  2. brew install pyenv
  3. PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.10.3 && pyenv global 3.10.3 && echo export PATH="$(pyenv root)/shims:$PATH" >> ~/.bash_profile && . ~/.bash_profile && pip install pipenv

Pip Installation

In the terminal

  1. Cd into the root of your test project
  2. Run pip install api-automation-tools

Local Setup

In the terminal

  1. Fork and clone this repository
  2. Cd into the root of your test project
  3. Run pip install -e path_to_apiautomationtools

Usage

Helpers

import apiautomationtools.helpers.dictionary_helpers as dh
import apiautomationtools.helpers.directory_helpers as dh
import apiautomationtools.helpers.json_helpers as js

Pytest

This class setup creates boilerplate data directories for tests and credentials. The teardown performs validations and organizational restructuring on any test generated data.

import pytest
from apiautomationtools.pytest import ApiPytestHelper

class SomeBasePytest(ApiPytestHelper):

    @pytest.fixture
    def test_app(self, ...):
        ...

Requests with AIOHTTP or HTTPX

These classes wrap easy to use methods for making async requests. All the standard requests request arguments are still applicable. As requests are completed, their data is saved in two csv files. One csv file contains the actual data sent, while the other contains a scrubbed set of data. The scrubbed csv file can be stored and used for reference validation of subsequent runs.

from apiautomationtools.client import AsyncRequests, HttpxRequests

aiohttp_requests = AsyncRequests()
httpx_requests = HttpxRequests()

batch = {'method': 'get', 'headers': {...}, 'url': '...', ...any classic requests arguments}
response = aiohttp_requests.request(batch)
response = httpx_requests.request(batch)

or

batch = [{'method': 'get', ...}, ...]
responses = aiohttp_requests.request(batch)
responses = httpx_requests.request(batch)

or 

batch = generate_batch("get", {...}, "https://httpbin.org/get")
response = aiohttp_requests.request(batch)
response = httpx_requests.request(batch)

Note: You can indicate where the batch generator will start looking for path parameters by placing
      a semicolon (;) where the path parameters start e.g. https://httpbin.org/get;/param/value.

Validations

This class performs a difference between scrubbed csv files of the stored and live data generated from the responses of the request method. Any mismatches can be raised as errors.

from apiautomationtools.validations import Validations

validations = Validations()
mismatches = validations.validate_references()
mismatches = validations.validate_references(actual_refs={...})
mismatches => [{'keys': ..., 'actual_refs': {...}, 'expected_refs': {...}, 'unscrubbed_refs': {...}}, ...]

Examples

An example of a test illustrating single and batch style requests - test_get_example.py

An example of a base class showing setups and teardowns - api_base_pytest_example.py

An example of the csv report file generated from running the test - get_example.csv

An example of the scrubbed csv report file generated from running the test - get_example_scrubbed.csv

An example of the log file generated from running the test - get_example.log

An example of the error csv report file generated from running the test - get_example_errors.csv

Directory structure

This package requires the following base structure for the project.

.
├── credentials                         # Optional - credentials
│   └── credentials.json                # Optional - credentials as json
├── tests                               # Required - test files
│   ├── data                            # Optional - test data
│   │   └── data.json                   # Optional - test data as json
│   └── test_file.py                    # Required - pytest test
└── validations                         # Optional - validation data
    └── file.json                       # Optional - validation data as json (The validation file's are scrubbed files 
                                                     autogenerated from test runs. However, the validation files 
                                                     organizational structure much match the structure of the test 
                                                     files in the tests directory.)

Metadata

Release files for api-automation-tools 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for api-automation-tools 2.0.0
File Size Uploaded Provenance Actions
api-automation-tools-2.0.0.tar.gz 27.7 kB Without provenance Details for api-automation-tools-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for api-automation-tools 2.0.0
File Interpreter ABI Platform Actions
api_automation_tools-2.0.0-py3-none-any.whl Python 3 none any Details for api_automation_tools-2.0.0-py3-none-any.whl

Total release size: 70.8 kB

Release files / api-automation-tools-2.0.0.tar.gz

Download URL api-automation-tools-2.0.0.tar.gz
Size 27.7 kB
Tags Source
SHA-256 checksum
How to use checksums
89e1167f9c8f1ef6977697c89cb6210efb77f516d9aa37c1ddc9e7664cbbac6c
BLAKE2b-256 checksum
How to use checksums
a7bc778e66e1f6633667165447b270f752554ad4e5247e29babaa628e9af70c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.3

Release files / api_automation_tools-2.0.0-py3-none-any.whl

Download URL api_automation_tools-2.0.0-py3-none-any.whl
Size 43.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e273c4d08e7573ebfc6850b9bf3fca462178a0464d90a8a2267b26111401f24
BLAKE2b-256 checksum
How to use checksums
49e23c6254fce47038b25dcd87ff9f853ef6112db04888481a03f760d38370da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.3

Security for api-automation-tools 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page