Languages, libraries and tools we used
- Python3
- pip
- Requests
- JsonPath
- Pytest
- Pycharm
Setup and installation
First need to install python3. If python isn't already installed, then go to python.org and download the latest version of python for your OS and run the installer. Or alternative on mac and have homebrew already installed then you can install python using below command
brew install python
Let’s test to make sure python is installed and available from the command line
python3 --version
Install requests Use the command below to install any Python module using Pip. But before that need to install 'pip'. PIP is the package management tool for Python libraries. If we’re using Python on your Mac to create applications, you’ll want to install PIP to easily install and use these libraries and software packages.
Using the Ensurepip Method to Install PIP on Mac for Python 3 If you’re using Python 3.4 or later, you can use the ensurepip command. This is the official method for installing PIP in Python, providing a secure method to install the application (if required).
To use the get-pip script to install PIP on Mac::
- Open the Terminal app via the Launchpad.
- In the Terminal, type
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
and press Enter. 3. Allow curl time to download the script onto Mac. Once it’s done, type
python3 get-pip.py
and press Enter. Allow time for the installation to complete.
To check your PIP version on a Mac:
- Open the Terminal app.
- In the Terminal, type
pip –version
and it doesn't show the Version, just do
pip3 -version
and press Enter.
Install requests To install any python module using pip3 you can use below command
python3 -m pip install requests
Install requests which we would use to actually make HTTP requests.
We need one more module called jsonpath is installed by executing below.
pip3 install jsonpath
We can always check the package is installed by executing below.
pip3 freeze
Will also use pytest as the test framework of choice.
pip3 install pytest
REST API Testing In Python Download PycharmPyCharm Professional Edition. Install Pycharm Open New project.
GO to Preference > Python Interpreter and install requests and jsonpath package.
Create Package
First create a python package named "api_test". Then create Python File named "get_user"
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
Generate Report using Allure
python3 -c "import nltk; nltk.download('all')"
pytest --alluredir=allure_reports
allure serve allure_reports
Generate Report using Pytest
pytest
pytest -rA
pip3 install pytest-html
pytest --html=Report/REPORT.html