How to create Python Allure Report for Rest API

HOME

pip install -U requests

pip install -U pytest

pip install allure-pytest

import allure
import requests

ENDPOINT = 'https://reqres.in/api/users'


@allure.description("This tests validates that the response returns status code 201")
@allure.severity(allure.severity_level.BLOCKER)
@allure.label("Owner", "Vibha Singh")
def test_create_user():
    request_body = {
        "name": "Vibha",
        "Job": "CEO"
    }

    response = requests.post(ENDPOINT, request_body)
    response_body = response.json()
    print("Request :", request_body)
    print("Response :", response_body)
    assert response.status_code == 201


@allure.description("This tests validates that the id in the response")
@allure.severity(allure.severity_level.NORMAL)
@allure.label("Owner", "Vibha Singh")
def test_response():
    request_body = {
        "name": "Vibha",
        "Job": "CEO"
    }

    response = requests.post(ENDPOINT, request_body)
    assert response.status_code == 201
    response_body = response.json()
    print("Request :", request_body)
    print("Response :", response_body)

    id = response_body["id"]
    if "id" in response_body:
        print("Value of id :", id)
    else:
        print("id not found")


@allure.description("This tests validates that the header in the response")
@allure.severity(allure.severity_level.NORMAL)
@allure.label("Owner", "Vibha Singh")
def test_header_in_request():
    request_body = {
        "name": "Vibha",
        "Job": "CEO"
    }

    headers = {'Content-Type': 'application/json; charset=utf-8'}
    response = requests.post(ENDPOINT, request_body, headers)
    print("Request :", request_body)
    print("Response :", response.json())
    print("Headers :", response.headers)
    assert response.headers["Content-Type"] == "application/json; charset=utf-8"


@allure.description("This tests validates that the name in the response - FAIL")
@allure.severity(allure.severity_level.NORMAL)
@allure.label("Owner", "Vibha Singh")
def test_verify_name():
    request_body = {
        "name": "Vibha",
        "Job": "CEO"
    }

    header = {"Content-Type":  "application/json; charset=utf-8"}
    response = requests.post(ENDPOINT, request_body, header)
    response_body = response.json()
    print("Response :", response_body)
    print("Request Header :", response.request.headers)
    assert response_body["name"] == "Test"

pytest --alluredir=<path to report directory> test.py

pytest --alluredir=C:\Users\Documents\Vibha\Automation\Python\AllureReport_Python\AllureReport Requests.py

allure serve <path to report directory>
allure serve C:\User\Documents\Vibha\Automation\Python\AllureReport_Python\AllureReport

Leave a comment