An API, or Application Program Interface, enables developers to integrate one app with another. To use an API, you make a request to a remote web server, and retrieve the data you need. Test Automation for APIs is needed to eliminate any possible errors, detect defects early, and ensure quality through the application development cycle. In this project, we’ll take a look at the automating API tests using Python.
- Main Objectives
- Official Documentation Sources
- Additional Tech Info Resources
- Dev Environment
- Nice to have tools
- About REST API app
- REST API app setup
- API endpoints and examples
- General guidelines: How to set up dev environment
- Tech Issues and Problem Solving
- Creating REST API Automation
- Cover following well known HTTP methods are commonly used in REST: GET, PUT, DELETE, POST.
- Build fast and readable automation using minimal code
- Industry-ready test structure
- Build readable test report using Allure Framework
- Test code should avoid violating principles like DRY, YAGNI, SRP and KISS
- Using cloud automation platforms: Codacy, CirclerCI, TravisCI, INSPECODE, Codecov
- Representational State Transfer (REST)
- Designing a RESTful API with Python and Flask
- Allure Test Report
- cars-api
- Practice API automation
- Flask
- Practice REST API automation with Python 3’s documentation
- Testing Flask Applications
- REST API Tutorial
- Full Pytest documentation
- PyCharm - Manage dependencies using requirements.txt
- Allure Framework
- Automated REST API Testing with Python
- Writing Unit Tests for REST API in Python
- Guide-to automating API tests using Python
- Testing Flask application with basic authentication
- Python 3.7.4
- PyTest 5.1.1
- Win 10 (64 bit)
- PyCharm 2019.2 (Community Edition)
- GitHub Desktop 2.1.2
- GIT 2.22.0.windows.1
Full list of dependencies see here.
- Install Python
- Install PyCharm
- Configure Python virtual environment and activate it
- Install Python prerequisites/packages
- Setup REST API app
NOTE: for more detailed info please see "Tech Issues and Problem Solving" section
This REST application written in Python was built to help testers learn to write API automation. The application has endpoints for you to practice automating GET, POST, PUT and DELETE methods. We have also included permissioning and authentication too. This web application was developed by Qxf2 Services.
- In your terminal prompt, pip install flask
- Copy the contents of this file and save it (anywhere) as cars_app.py
- In your terminal prompt, cd directory_that_has_cars_app_py
- In your terminal prompt, python cars_app.py
If everything goes right, you should see an output similar to the following:
* Serving Flask app "cars_app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 105-519-712
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
- For more details please click here
Changing the project interpreter in the PyCharm project settings
- In the Settings/Preferences dialog (Ctrl+Alt+S), select Project | Project Interpreter.
- Expand the list of the available interpreters and click the Show All link.
- Select the target interpreter. When PyCharm stops supporting any of the outdated Python versions, the corresponding project interpreter is marked as unsupported.
- The Python interpreter name specified in the Name field, becomes visible in the list of available interpreters. Click OK to apply the changes.
For more info please check here
PyCharm - Choosing Your Testing Framework
- Open the Settings/Preferences dialog, and under the node Tools, click the page Python Integrated Tools.
- On this page, click the Default Test Runner field.
- Choose the desired test runner:
For more info please see Enable Pytest for you project
Setting up Python3 virtual environment on Windows machine
- open CMD
- navigate to project directory, for example:
cd C:\Users\superadmin\Desktop\Python\CodinGame
- run following command:
pip install virtualenv
- run following command:
virtualenv venv --python=python
Setting up Python3 virtual environment on Linx (Ubuntu) machine
- Install pip first
sudo apt-get install python3-pip
- Then install virtualenv using pip3
sudo pip3 install virtualenv
- Now create a virtual environment
virtualenv venv
you can use any name insted of venv
- You can also use a Python interpreter of your choice:
virtualenv -p /usr/bin/python2.7 venv
- Active your virtual environment:
source venv/bin/activate
- Using fish shell:
source venv/bin/activate.fish
- To deactivate:
deactivate
- Create virtualenv using Python3:
virtualenv -p python3 myenv
- Instead of using virtualenv you can use this command in Python3:
python3 -m venv myenv
Activate Virtual Environment
In a newly created virtualenv there will be a bin/activate shell script. For Windows systems, activation scripts are provided for CMD.exe and Powershell.
- Open Terminal
- Run: \path\to\env\Scripts\activate
Auto generate requirements.txt
Any application typically has a set of dependencies that are required for that application to work. The requirements file is a way to specify and install specific set of package dependencies at once.
Use pip’s freeze command to generate a requirements.txt file for your project:
pip freeze > requirements.txt
If you save this in requirements.txt, you can follow this guide: PyCharm - Manage dependencies using requirements.txt, or you can:
pip install -r requirements.txt
error: RPC failed; curl 56 Recv failure: Connection was reset
- Open Git Bash
- Run: "git config --global http.postBuffer 157286400"
How to fix in case .gitignore is ignored by Git
Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore
NOTE:
- First commit your current changes, or you will lose them.
- Then run the following commands from the top folder of your Git repository:
git rm -r --cached .
git add .
git commit -m "fixed untracked files"
How to generate Allure report with history trends (Windows OS)
Step by step:
- Run tests from pytest using following arguments: -v --alluredir=allure-results
- Copy '.\allure-report\history' folder into '.\allure-results\history'
- Run: allure generate .\allure-results\ -o .\allure-report\ --clean
- Following output should appear: Report successfully generated to .\allure-report
- Run: allure open .\allure-report\
Sphinx Documentation Set Up
Step by step:
- Create docs directory
- Open cmd > Go to docs directory
- cmd > Run: sphinx-quickstart Note: run with default answers
- Go to docs/conf.py
- Uncomment following lines:
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
- Update extensions list as following:
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc']
- Update template as following:
html_theme = 'sphinx_rtd_theme'
- Update sys.path.insert as following:
sys.path.insert(0, os.path.abspath('..'))
- Go to docs/index.rst > add modules, see example below:
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
- Open cmd > run:
sphinx-apidoc -o . ..
- cmd > Run: make html
- Install html template:
pip install sphinx_rtd_theme
Auto-Generated Python Documentation with Sphinx
Step by step:
- Open CMD
- Go to docs directory
- Run: make clean
- Run: make html