-
Notifications
You must be signed in to change notification settings - Fork 191
Email pytest report with Gmail
Email pytest report (email_pytest_report.py) script designed to send pytest test report email. It uses Gmails smtplib library.
- It supports both text and html formatted messages
- It supports text, html, image, audio files as an attachment
-
Before sending email, we need to generate the pytest report. To generate html formatted test report, you need to use pytest-html plugin.
To install it use command:
pip install pytest-html
-
Now set credentials and other details under .env file. Refer env_conf file available at root of repo. For this script to work, we need to set following details under .env file at root of the repository.
#Details needed for the Gmail
#Fill out the email details over here
app_username ='USERNAME' #this should be your email address
#Login has to use the app password because of Gmail security configuration
# 1. Setup 2 factor authentication
# 2. Follow the 2 factor authentication setup wizard to enable an app password
#Src: https://support.google.com/accounts/answer/185839?hl=en
#Src: https://support.google.com/mail/answer/185833?hl=en
app_password = 'APP_PASSWORD'
#Details for sending pytest report
smtp_ssl_host = 'smtp.gmail.com' # Add smtp ssl host of your email client
smtp_ssl_port = 465 # Add smtp ssl port number of your email client
sender = 'abc@xyz.com' #Add senders email address here
targets = ['asd@xyz.com','qwe@xyz.com'] # Add recipients email address in a list
-
To generate the app password, you need to setup 2 factor authetication and enable an app password.
Refer following resources for it:
#Src: https://support.google.com/accounts/answer/185839?hl=en
-
To email test report with our framework use following command from the root of repo
- e.g.
pytest -s -v --email_pytest_report y --html=log/pytest_report.html
A simple IMAP scripts is used to interact with IMAP servers. You can connect to your IMAP host, login to the email, fetch latest messages in the inbox, search for a subject and return the latest unique ids of the emails, fetch the email body for a given uid etc., All the parameters needed for this script are maintained in env_conf file and rename the file to .env .
#---EXAMPLE USAGE---
if __name__=='__main__':
#Fetching conf details from the conf file
imap_host = os.getenv(imaphost)
username = os.getenv(app_username)
password = os.getenv(app_password)
#Initialize the email object
email_obj = Email_Util()
#Connect to the IMAP host
email_obj.connect(imap_host)
#Login
if email_obj.login(username,password):
print ("PASS: Successfully logged in.")
else:
print ("FAIL: Failed to login")