API Test Automation with Python

Amit Yerva
4 min readJul 19, 2021

--

Setting up an API automation application using Python.

In this story, we are going to see how to setup python test automation application.

Set up environment:

Installing pip with get-pip.py:

The first thing to do after setting up python3 is to install pip. pip is the reference Python package manager. It’s used to install and update packages. You’ll need to make sure you have the latest version of pip installed. Assuming that pip is never installed on your system previously,

  1. Securely Download get-pip.py
  2. Run python get-pip.py. This will install or upgrade pip. Additionally, it will install setuptools and wheel if they’re not installed already.

If pip is already installed, ensure that the library is up to date with :

python3 -m pip install --upgrade pip setuptools wheel

Setting venv:

Venv is available by default in Python 3.3 and later, and installs pip and setuptools into created virtual environments. The venv module provides support for creating lightweight virtual environments with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary and can have its own independent set of installed Python packages in its site directories.

Creating virtual environment:

Running this command creates the target directory and places a pyvenv.cfg file in it with a home key pointing to the Python installation from which the command was run. It also creates a bin (or Scripts on Windows) subdirectory containing a copy/symlink of the Python binary/binaries.

Unix: python3 -m venv myvenvWindows: py -m venv myenv

Before you can start installing or using packages in your virtual environment you’ll need to activate it.

Unix: source myenv/bin/activateWindows: .\myenv\Scripts\activate

Now that we have an environment, lets install packages.

Let’s install the Requests library from the Python Package Index (PyPI):

Unix: python3 -m pip install requestsWindows: py -m pip install requests

Install pytest:

PyTest is a testing framework that allows users to write test codes using Python programming language. It helps you to write simple and scalable test cases for databases, APIs, or UI. It helps to write tests from simple unit tests to complex functional tests.

pip install pytestpip install pytest-steps

Install jsonschema:

JSON Schema is a grammar language for defining the structure, content, and (to some extent) semantics of JSON objects. It lets you specify metadata (data about data) about what an object’s properties mean and what values are valid for those properties.

pip install jsonschema

HTTP requests:

The requests library is used for sending HTTP requests and we receive the response object in return. Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your PUT & POST data, just use the json method.

session = requests.session()response = session.get(url, params=params, verify=False)

Session object allows one to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance and will use urllib3’s connection pooling. So if several requests are being made to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase.

For a post:

response = session.post(url, data=data, headers=headers, params=params, **kwargs)

Modules with test cases: test_open_weather_map.py

We can write the steps and logical abstraction in a separate python module. Where for each of the test files we will have one steps file.

Python data structures at use:

We can utilise lists, dictionaries in python to form the request payload. We can convert a json to string and vice versa.

json.dumps() function converts a Python object into a json string.

json.loads() method can be used to parse a valid JSON string and convert it into a Python Dictionary.

Assertions:

We can include assertions for following to mark the test case results:

  • Response body schema validation: Use validate function from jsonschema. If validation fails, it will throw an error.
validate(instance=json_data, schema=schema)

Here is the response for the get request to openweathermap api.

{
"coord": {
"lon": 73.8553,
"lat": 18.5196
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10n"
}
],
"base": "stations",
"main": {
"temp": 298.02,
"feels_like": 298.73,
"temp_min": 298.02,
"temp_max": 298.02,
"pressure": 1007,
"humidity": 83,
"sea_level": 1007,
"grnd_level": 946
},
"visibility": 10000,
"wind": {
"speed": 3.83,
"deg": 268,
"gust": 6.74
},
"rain": {
"1h": 0.44
},
"clouds": {
"all": 100
},
"dt": 1625759448,
"sys": {
"country": "IN",
"sunrise": 1625704424,
"sunset": 1625751908
},
"timezone": 19800,
"id": 1259229,
"name": "Pune",
"cod": 200
}

For the above response object, we can form the jsonschema using online tools provided for converting the json response to metadata/ jsonschema.

We can omit the repetitive elements in an json list in json object. Single element should be enough to generate the metadata. Having multiple elements will increase the metadata unnecessarily. e.g., Liquid-technologies

  • Response message and status code validation: Convert the response object received from the GET request to json() and fetch the required key for validation. As in here the response contains a message.
assert (response.json()['message'] == 'Successful')
assert (response.status_code == 200)

Running the Test cases:

We can run tests using pytest. The modules prefixed with test_ can be ran directly using pytest.

pytest ~/api_automation/test_cases/test_open_weather_map.py

Run particular test class or test cases method using:

pytest api_automation/test_cases/test_open_weather_map.py::OpenWeathermap::test_get_weathermap_city

Future scope for enhancement:

  • We Can run test cases in parallel if the suit takes time after adding more test cases. For this, we can use pytest-xdist.
  • Generating html test results using pytest-html. Using this we can generate the html report of tests run.

References:

Pip and venv setup:

https://packaging.python.org/tutorials/installing-packages/ https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

Pytest: https://www.guru99.com/pytest-tutorial.html

Json schema: https://restfulapi.net/json-schema/.

https://json-schema.org/

Requests: https://www.w3schools.com/python/module_requests.asp

--

--