API testing with Python + Pytest + Allure | QAProvider.com
Subscribe me on new discussions

API testing with Python + Pytest + Allure

Views: 3068

How to write a first API test with Python + Pytest + Allure

Plan:

  • Install pytest as a tests framework
  • Install Allure as a nice test report tool
  • Write API test for Github API
  • Run all together having beautiful report

Start with:

  • Installed ubuntu 20.04 focal (running on vagrant, box is ubuntu/focal64)
  • Python 3.8.5 installed by default with this box

Scripts could be found on QAProvider github

Create virtual environment

Update package information

$ sudo apt-get update

Install virtual environment

$ sudo apt install python3-venv

Go to working directory (/code/pytests in my example)

$ cd /code/pytests

Create virtual environment with name api-tests

$ python3 -m venv api-tests

If you work on windows environment with vagrant then you might have got next error: Error: [Errno 71] Protocol error: 'lib' -> '/code/pytests/api-tests/lib64'. In this case you need to create virtual environment with --always-copy option using next command:

$ virtualenv -p python3 api-tests --always-copy

Activate virtual environment

$ source api-tests/bin/activate

Installing Allure

We need to install Allure2. Following the instruction on Allure Github we need brew to install Allure. Let's start with brew installation. The good article on how to do this on Ubuntu is here.

Install Ruby

$ sudo apt install ruby

Install brew

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install)"

Add Linux-brew to your ~/.bash_profile by running

$ echo 'export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"' >>~/.bash_profile

$ echo 'export MANPATH="/home/linuxbrew/.linuxbrew/share/man:$MANPATH"' >>~/.bash_profile

$ echo 'export INFOPATH="/home/linuxbrew/.linuxbrew/share/info:$INFOPATH"' >>~/.bash_profile

Add Linuxbrew to your PATH

$ PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"

Finally install Allure

$ brew install allure

Check the version

$ allure --version 
2.13.6

Installing Pytest

$ pip install -U pytest

Check the version

$ pytest --version 
pytest 6.1.1 

Install allure-pytest

$ pip install allure-pytest

Write tests

We will write 2 tests for get a user function of Github users API. 1 success test & 1 failed test just to see the difference in results.

Full example could be found here on Github

Complete project structure will look like that:

library is a folder with our small package to do an API request and have all stuff for Allure report

test_github.py is a file with 2 tests: test_success and test_failed

Run tests and check results

Run tests

$ pytest --alluredir=allure_results

server results with allure

$ allure serve allure_results/ 
Generating report to temp directory... 
Report successfully generated to /tmp/17631871144287129490/allure-report 
Starting web server... 
2020-10-21 23:40:31.094:INFO::main: Logging initialized @1810ms to org.eclipse.jetty.util.log.StdErrLog 
Can not open browser because this capability is not supported on your platform. You can use the link below to open the report manually. 
Server started at . Press to exit

Open results in a browser. As we build it under Vagrant on Windows machine it could be opened with 192.168.10.22 IP address instead of 127.0.0.1.

First, you will see overview panel.

Success result. You will see detailed information in the report: request, method, url, headers, response.

Failed result. You will see detailed information in the report: request, method, url, headers, response (you need to collapse it and it will be the same as in success result above) and failed assert.

Any questions or suggestions?

Karma: {{ total }}
Published: 2 years ago by QAProvider Team
Roman at 2020-10-25 20:32:00
Karma: {{ total }}

Useful information