Select Page
Automation Testing

Customized Test Automation PDF Report

In this blog article, you will learn how to create Customized Automation Testing report using Behave & Python.

Customized Test Automation PDF Report

Generating Customized Test Automation PDF report is a great value add to your team. Let’s say for instance if you wish to share a high-level automation test report with your business stakeholders. Then emailing the report as a PDF is a simple and viable solution. However, sending predefined report or technical information is insignificant and uninteresting to the business. As a software testing services company, we use different report templates for different audiences. In this blog article, you will learn how to create Customized Automation Testing report using Behave & Python.

PDF Report Package Installation

pip install pdf_reports
  

For Windows users: If you see OSError: dlopen() failed to load a library: cairo / cairo-2, then you need to install GTK+. Refer the following URL for more details – Install GTK+ with the aid of MSYS2

Generating Behave JSON Outputs

In this blog article, we are using behave framework JSON output to create PDF report. To create JSON report from behave, use the below command.

behave -f json -o reports.json
  

Consuming the JSON Output

Create a python file and paste the below code.

from pdf_reports import pug_to_html, write_report
import json
file = open(r'../reports.json')
json_report = json.load(file)
html = pug_to_html("template.pug", title1="My report", json_report=json_report)
write_report(html, "example.pdf")
  
Code Explanation

Line #1 & #2 – Package Imports

Line #3 – Opening the json output file which is generated by behave framework.

Line #4 – Loading the json file

Line #5 – Calling pug_to_html method to generate the PDF report.

Note – To generate the report, you need template.pug file. Please refer the pug_to_html method call, you can see template.pug file as one of the arguments.

Template.pug file

Pug is a template engine. The PDF_Reports package uses Pug to generate the PDF output. Let’s see how to create the template file in your project.

1) File Creation – Create & open template.pug file in your project

2) Report title – The below line creates an H1 tag and applies Semantic UI CSS.

h1.ui.header Behave Report
  

3) Sidebar – Creating sidebar in the report which enhances the look and feel.

#sidebar: p Test Automation Report
  

4) Iterate features from the json data

each feature in json_report
    if feature.keyword =='Feature'
        div.ui.piled.segment
            h4.ui.header Feature: {{feature.name}}
  

5) Iterate Scenarios

each scenario in feature.elements
  if scenario.type =='scenario'
  h5.ui.header Scenario: {{scenario.name}}
  

6) Iterate Steps

table.ui.olive.table
 thead
  tr
   th Step Type
   th Description
   th Status
   th Duration   
 each step in scenario.steps
  tr
   td {{step.keyword}}
   td {{step.name}}
   if step.result.status == 'passed'
    td.positive.center.aligned Passed
   else
    td.negative.center.aligned Failed
   td {{step.result.duration}}
  

7) Run the python file which is calling pug_to_html method. If the report is generated successfully, you can see the ouput as shown below.

Customized Test Automation PDF Report

Full Code
h1.ui.header Behave Report

#sidebar: p Test Automation Report

each feature in json_report
    if feature.keyword =='Feature'

        div.ui.piled.segment
            h4.ui.header Feature: {{feature.name}}
            each scenario in feature.elements
                if scenario.type =='scenario'
                    h5.ui.header Scenario: {{scenario.name}}
                    table.ui.olive.table
                        thead
                            tr
                                th Step Type
                                th Description
                                th Status
                                th Duration
                        each step in scenario.steps
                            tr
                                td {{step.keyword}}
                                td {{step.name}}
                                if step.result.status == 'passed'
                                    td.positive.center.aligned Passed
                                else
                                    td.negative.center.aligned Failed
                                td {{step.result.duration}}
  

In ConclusionThe above template is ( quite simple / very straightforward,) it does not have implementation to handle BDD Scenario Outline, Tags, and Screenshots. As an automation testing company, we use comprehensive Pug reporting template to generate PDF reports. Once the template is in place, you can create multiple reports for different audiences. For successful test automation, make your reports easily accessible to everyone in the team.

Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility