API Testing Automation using Python, Introduction, configuration, and installation of the Python
Published: 2022-11-29 08:46:16

**<u>Languages, libraries and tools we used</u>**

- Python3
- pip
- Requests
- JsonPath
- Pytest
- Pycharm

**<u>Setup and installation</u>**

First need to install python3. If python isn't installed, then go to [python.org](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.

2. In the terminal window, copy and paste the following command:

```
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
```
3. Press the Enter key to run the command. This will download and run the installation script for Homebrew.

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

5. 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
```
Let’s test to make sure python is installed and available,  from that RUN the command

```
python3 --version
```
**<u>Install requests</u>**
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**:



**<u>To use the get-pip script to install PIP on Mac::</u>**

```
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
```
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.

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

```
pip --version
```
 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
```
This command shows that pip version.

**<u>Install requests</u>**
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 
```

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

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gm7lxcpqdgjhtjzgntx4.png)



**<u>REST API Testing In Python</u>**
Download [Pycharm](https://www.jetbrains.com/pycharm/)PyCharm Professional Edition. Install Pycharm Open New project.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dth223on04fmdxydxmjm.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvi5en81hr0v3yvl2ftv.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfdqwo4e3md33sqnmvx3.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ta2w57wxtp8hlqngkj3e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6so3ifk77jka9zppo8v1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8a8eb06b2hnzqc5e427c.png)



**<u>Create Package</u>**

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zeiextcd7d5jj1syekkr.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5y1vdt0wbzvqina55e3.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kevrwgevo8tk8z16gyqk.png)



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
```
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5ufmby8g7wqajtr7kw6.png)

**<u>Generate Report using Allure</u>**


```
python3 -c "import nltk; nltk.download('all')"
```

```
pytest --alluredir=allure_reports
```

```
allure serve allure_reports
```
Link- https://github.com/and-buk/api-test

**<u>Generate Report using Pytest</u>**

```
pytest
```

```
pytest -rA
```

```
pip3 install pytest-html
```

```
pytest --html=Report/REPORT.html
```
link - https://github.com/ashrika786/api-testing-python