/REST_API_AUTOMATION

Practice REST API test automation with Python 3

Primary LanguagePythonThe UnlicenseUnlicense

License: Unlicense HitCount Documentation Status Codacy Badge CircleCI Build Status codecov Test Coverage Maintainability

Practice REST API test automation with Python 3

Project Description

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.

Table of Contents:

  1. Main Objectives
  2. Official Documentation Sources
  3. Additional Tech Info Resources
  4. Dev Environment
  5. Nice to have tools
  6. About REST API app
  7. REST API app setup
  8. API endpoints and examples
  9. General guidelines: How to set up dev environment
  10. Tech Issues and Problem Solving

Main Objectives:

  • 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

Official Documentation Sources

Additional Tech Info Resources

Dev Environment:

Python Packages

Full list of dependencies see here.

Nice to have tools

  1. Fiddler: The free web debugging proxy
  2. Kite: Code Faster in Python

General guidelines: How to set up dev environment.

  1. Install Python
  2. Install PyCharm
  3. Configure Python virtual environment and activate it
  4. Install Python prerequisites/packages
  5. Setup REST API app

NOTE: for more detailed info please see "Tech Issues and Problem Solving" section

About REST API app

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.

REST API app setup

  1. In your terminal prompt, pip install flask
  2. Copy the contents of this file and save it (anywhere) as cars_app.py
  3. In your terminal prompt, cd directory_that_has_cars_app_py
  4. 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)

API endpoints and examples

Tech Issues and Problem Solving:

Changing the project interpreter in the PyCharm project settings
  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Project | Project Interpreter.
  2. Expand the list of the available interpreters and click the Show All link.
  3. Select the target interpreter. When PyCharm stops supporting any of the outdated Python versions, the corresponding project interpreter is marked as unsupported.
  4. 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
  1. Open the Settings/Preferences dialog, and under the node Tools, click the page Python Integrated Tools.
  2. On this page, click the Default Test Runner field.
  3. Choose the desired test runner:

For more info please see Enable Pytest for you project

Setting up Python3 virtual environment on Windows machine
  1. open CMD
  2. navigate to project directory, for example:
cd C:\Users\superadmin\Desktop\Python\CodinGame
  1. run following command:
pip install virtualenv
  1. run following command:
virtualenv venv --python=python
Setting up Python3 virtual environment on Linx (Ubuntu) machine

How to install virtualenv

  1. Install pip first
    sudo apt-get install python3-pip
  1. Then install virtualenv using pip3
    sudo pip3 install virtualenv
  1. Now create a virtual environment
    virtualenv venv

you can use any name insted of venv

  1. You can also use a Python interpreter of your choice:
    virtualenv -p /usr/bin/python2.7 venv
  1. Active your virtual environment:
    source venv/bin/activate
  1. Using fish shell:
    source venv/bin/activate.fish
  1. To deactivate:
    deactivate
  1. Create virtualenv using Python3:
    virtualenv -p python3 myenv
  1. Instead of using virtualenv you can use this command in Python3:
    python3 -m venv myenv

Source

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.

  1. Open Terminal
  2. Run: \path\to\env\Scripts\activate

Source

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

Source

error: RPC failed; curl 56 Recv failure: Connection was reset
  1. Open Git Bash
  2. Run: "git config --global http.postBuffer 157286400"

Source

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:

  1. Run tests from pytest using following arguments: -v --alluredir=allure-results
  2. Copy '.\allure-report\history' folder into '.\allure-results\history'
  3. Run: allure generate .\allure-results\ -o .\allure-report\ --clean
  4. Following output should appear: Report successfully generated to .\allure-report
  5. Run: allure open .\allure-report\

Source

Sphinx Documentation Set Up


Step by step:

  1. Create docs directory
  2. Open cmd > Go to docs directory
  3. cmd > Run: sphinx-quickstart Note: run with default answers
  4. Go to docs/conf.py
  5. Uncomment following lines:
    import os
    import sys
    sys.path.insert(0, os.path.abspath('.'))
  1. Update extensions list as following:
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc']
  1. Update template as following:
html_theme = 'sphinx_rtd_theme'
  1. Update sys.path.insert as following:
sys.path.insert(0, os.path.abspath('..'))
  1. Go to docs/index.rst > add modules, see example below:
.. toctree::
   :maxdepth: 2
   :caption: Contents:

   modules
  1. Open cmd > run:
sphinx-apidoc -o . ..
  1. cmd > Run: make html
  2. Install html template:
pip install sphinx_rtd_theme

Video Tutorial Sphinx Documentation More Info

Auto-Generated Python Documentation with Sphinx


Step by step:

  1. Open CMD
  2. Go to docs directory
  3. Run: make clean
  4. Run: make html

Source