RelationDigest

Friday, 2 February 2024

How to create Python Allure Report for Rest API

Read on blog or Reader
Site logo image vibssingh posted: " HOME In this tutorial, we will create an Allure Report for Rest API in Python. Prerequisite: Python is installed on the machine PIP is installed on the machine PyCharm or other Python Editor is installed on the machine If you need hel" QA Automation Expert Read on blog or Reader

How to create Python Allure Report for Rest API

vibssingh

February 2

HOME

In this tutorial, we will create an Allure Report for Rest API in Python.

Prerequisite:

  1. Python is installed on the machine
  2. PIP is installed on the machine
  3. PyCharm or other Python Editor is installed on the machine

If you need help in installing Python, please refer to this tutorial - How to Install Python on Windows 11.

If you need help in installing PyCharms, please refer to this tutorial - How to install PyCharms on Windows 11.

Installation of Python Requests Module

Step 1 - We are using PyCharm IDE, so we need to install the Request library. Use the below command using pip.

 pip install -U requests 

Step 2 - Install Pytest

 pip install -U pytest 

Step 3 - Install Allure Report for Python

 pip install allure-pytest 

Step 4 - Create a new project folder and open it in PyCharm.

Step 5 - Go to the project folder and Create a new directory - tests.

Step 6 - Add allure-pytest package to the PyCharms

Go to File->Settings ->Project: AllureReport_Python->Python Interpreter.

Click on the "+" sign and enter allure-r in the search bar. It will show a list of packages. Select the "allure-pytest" package and click on the "Install Package".

Once the package is installed, we will see the message that the package is installed successfully.

Step 7 - Create the test.

Create a file - Requests.py and create the tests in that file.

 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" 

Step 8 - To run this test, use the below command:

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

In my case, I want the allure files to be generated in AllureReport folder as shown below:

Below is the command we use to run the tests. This will generate a directory to the specified path with some files in JSON format.

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

The output of the above program is

Step 9 - Generating Allure report

Now, we have the supported data that will be required to get the report, We just need to convert it into HTML so that we can see this on the browser.

Use the below command in the terminal/cmd:

 allure serve <path to report directory> 

I have used the below command:

 allure serve C:\User\Documents\Vibha\Automation\Python\AllureReport_Python\AllureReport 

It will start a jetty server within a few seconds and the report will be visible inside your browser.

The overview page hosts several default widgets representing the basic characteristics of your project and test environment.

  1. Statistics – overall report statistics.
  2. Launches – if this report represents several test launches, statistics per launch will be shown here.
  3. Behaviours – information on results aggregated according to stories and features.
  4. Executors – information on test executors that were used to run the tests.
  5. History Trend – if tests accumulated some historical data, it's trend will be calculated and shown on the graph.
  6. Environment – information on the test environment.

Categories in Allure Report

The categories tab gives you a way to create custom defect classifications to apply for test results. There are two categories of defects – Product Defects (failed tests) and Test Defects (broken tests).

Suites in Allure Report

On the Suites tab a standard structural representation of executed tests, grouped by suites and classes can be found.

Graphs in Allure Report

Graphs allow you to see different statistics collected from the test data: statuses breakdown or severity and duration diagrams.

Timeline in Allure Report

The timeline tab visualizes retrospective of tests execution, allure adaptors collect precise timings of tests, and here on this tab, they are arranged accordingly to their sequential or parallel timing structure.

Behaviours of Allure Report

This tab groups test results according to Epic, Feature, and Story tags.

Packages in Allure Report

The packages tab represents a tree-like layout of test results, grouped by different packages.

That's it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Comment
Like
Tip icon image You can also reply to this email to leave a comment.

QA Automation Expert © 2024. Manage your email settings or unsubscribe.

WordPress.com and Jetpack Logos

Get the Jetpack app

Subscribe, bookmark, and get real-time notifications - all from one app!

Download Jetpack on Google Play Download Jetpack from the App Store
WordPress.com Logo and Wordmark title=

Automattic, Inc. - 60 29th St. #343, San Francisco, CA 94110  

at February 02, 2024
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

The Only Correct Decision, Part III

Dropping the atomic bombs on Hiroshima and Nagasaki was not only justified, but the best possible decision given the circumstances. ͏     ­͏...

  • [New post] Wiggle Kingdom: April Earnings on Spring Savings!
    Betsi...
  • [New post] Balancing the ‘E’ and ‘S’ in Environment, Social and Governance (ESG) crucial to sustaining liquidity and resilience in the African loan market (By Miranda Abraham)
    APO p...
  • Something plus something else
    Read on bl...

Search This Blog

  • Home

About Me

RelationDigest
View my complete profile

Report Abuse

Blog Archive

  • August 2025 (25)
  • July 2025 (59)
  • June 2025 (53)
  • May 2025 (47)
  • April 2025 (42)
  • March 2025 (30)
  • February 2025 (27)
  • January 2025 (30)
  • December 2024 (37)
  • November 2024 (31)
  • October 2024 (28)
  • September 2024 (28)
  • August 2024 (2729)
  • July 2024 (3249)
  • June 2024 (3152)
  • May 2024 (3259)
  • April 2024 (3151)
  • March 2024 (3258)
  • February 2024 (3046)
  • January 2024 (3258)
  • December 2023 (3270)
  • November 2023 (3183)
  • October 2023 (3243)
  • September 2023 (3151)
  • August 2023 (3241)
  • July 2023 (3237)
  • June 2023 (3135)
  • May 2023 (3212)
  • April 2023 (3093)
  • March 2023 (3187)
  • February 2023 (2865)
  • January 2023 (3209)
  • December 2022 (3229)
  • November 2022 (3079)
  • October 2022 (3086)
  • September 2022 (2791)
  • August 2022 (2964)
  • July 2022 (3157)
  • June 2022 (2925)
  • May 2022 (2893)
  • April 2022 (3049)
  • March 2022 (2919)
  • February 2022 (2104)
  • January 2022 (2284)
  • December 2021 (2481)
  • November 2021 (3146)
  • October 2021 (1048)
Powered by Blogger.