DEV Community

Khairun Nahar Nowrin
Khairun Nahar Nowrin

Posted on • Edited on

API Testing Automation using Python, Introduction, configuration, and installation of the Python

Languages, libraries and tools we used

  • Python3
  • pip
  • Requests
  • JsonPath
  • Pytest
  • Pycharm

Setup and installation

First need to install python3. If python isn't installed, then go to python.org and download the latest version of python for your OS and Install it.
Or alternative on macOs using homebrew to install python. If you don't have homebrew then install it before install Python. To install Homebrew on your macOS system, you can follow these steps:

1.Open a terminal window on your macOS system. You can do this by searching for "Terminal" in the spotlight search, or by using the Terminal app in the Applications folder.

  1. In the terminal window, copy and paste the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Enter fullscreen mode Exit fullscreen mode
  1. Press the Enter key to run the command. This will download and run the installation script for Homebrew.

  2. Follow the instructions on the screen to complete the installation process. This may involve entering your password or providing other information.

  3. Once the installation is complete, you can use the brew command to manage the packages that are installed on your system. For example, you can use the brew search command to search for available packages, or the brew install command to install a package.

After that install python. Open terminal and RUN

brew install python3
Enter fullscreen mode Exit fullscreen mode

Let’s test to make sure python is installed and available, from that RUN the command

python3 --version
Enter fullscreen mode Exit fullscreen mode

Install requests
After that you need to install PIP. So what is pip?
pip is a package manager for Python. It is used to install and manage software packages written in Python.

The pip package manager is included with the latest versions of Python, but it may not be installed on your system by default. If you want to use pip to install and manage Python packages, you will need to install it on your system.

Installing pip is relatively straightforward, and you can do it using the curl or wget command, depending on which one is available on your system. Here's an example of how to install pip using curl:

To use the get-pip script to install PIP on Mac::

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
Enter fullscreen mode Exit fullscreen mode

Python Package Index (PyPI) website. The -o flag is used to specify the output filename, in this case get-pip.py.

Once the get-pip.py script has been downloaded, the python command is used to run it and install pip on your system. This is a simple and convenient way to install pip without having to manually download the script and run it yourself.

How to check your PIP version on a Mac:
To check the version of pip that is installed on your system, you can use the

pip --version
Enter fullscreen mode Exit fullscreen mode

command. This command will show you the version of pip that is currently installed, along with the version of Python that it is associated with.

and if it doesn't show the Version, just RUN this Command

pip3 -version
Enter fullscreen mode Exit fullscreen mode

This command shows that pip version.

Install requests
The requests library is a popular Python package for making HTTP requests. It is often used to send and receive data from web servers and APIs.
In short, installing the requests library can make working with HTTP requests in Python easier and more convenient, and it is often a recommended library to include in your Python projects.

Once you have installed pip, you can use it to install the requests library by running the following command:

python3 -m pip install requests 
Enter fullscreen mode Exit fullscreen mode

Next Install JSONPath. JSONPath provides a simple and convenient way to access specific parts of a JSON document, such as individual fields or nested objects. This can be useful when you only need to extract a small amount of data from a large JSON document or API response, or when you want to perform operations on specific parts of the data.

To install the jsonpath library in Python, you can use the pip package manager. Once you have installed pip, you can use it to install the jsonpath library by running the following command:

pip3 install jsonpath
Enter fullscreen mode Exit fullscreen mode

After that need to import requirements.txt. A requirements.txt file is a common way to specify the dependencies of a Python project. It is typically used in conjunction with the pip package manager to install the required packages and their versions.

The requirements.txt file contains a list of package names and versions, one per line. To install requirements.txt 1st need to install freeze. The pip freeze command generates a list of installed packages and their versions in a format that is compatible with the pip install command. This makes it easy to create a requirements.txt file for your project and share it with others, so they can use it to install the same packages and versions on their own systems.
Use the pip freeze command, you can simply run it in your terminal. This will print the list of installed packages and their versions to the terminal.

To save the list of packages to a requirements.txt file, you can use the > operator to redirect the output of the pip freeze command to a file, like this:

pip3 freeze
pip3 freeze > requirements.txt
pip3 install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

This will create a requirements.txt file in the current directory and write the list of installed packages and their versions to the file

After that install Pytest. Using a testing framework like Pytest can help you catch and fix bugs in your code before it is deployed, ensuring that it is of high quality and works as intended. Pytest also makes it easy to organize and manage your tests, allowing you to run specific tests or groups of tests, and providing detailed output and feedback when tests fail.

pip3 install pytest 
Enter fullscreen mode Exit fullscreen mode

Image description

REST API Testing In Python
Download PycharmPyCharm Professional Edition. Install Pycharm Open New project.

Image description

GO to Preference > Python Interpreter and install requests and jsonpath package.

Image description

Image description

Image description

Image description

Image description

Create Package

First create a python package named "api_test". Then create Python File named "get_user"

Image description

Image description

Image description

To build a Python REST API test suite, you will need to install Python3 first, and below packages (using pytest test framework in this example).

pip3 install -U requests Flask pytest pytest-html
Enter fullscreen mode Exit fullscreen mode

Image description

Generate Report using Allure

python3 -c "import nltk; nltk.download('all')"
Enter fullscreen mode Exit fullscreen mode
pytest --alluredir=allure_reports
Enter fullscreen mode Exit fullscreen mode
allure serve allure_reports
Enter fullscreen mode Exit fullscreen mode

Link- https://github.com/and-buk/api-test

Generate Report using Pytest

pytest
Enter fullscreen mode Exit fullscreen mode
pytest -rA
Enter fullscreen mode Exit fullscreen mode
pip3 install pytest-html
Enter fullscreen mode Exit fullscreen mode
pytest --html=Report/REPORT.html
Enter fullscreen mode Exit fullscreen mode

link - https://github.com/ashrika786/api-testing-python

Top comments (0)