Automatic e-mail function (fifty-three) advanced applications of test automation - integrated automatic email function

Essays and records to facilitate their access to fellow travelers.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install any additional information please search on their own here do not introduced, all examples are performed using python3.6 + selenium.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

Integrated automatic email function

After solving the previous problem, it can now automatically send e-mail function to inherit a test automation project. The following open runtest.py file to re-edit.

 

# !/usr/bin/env python
# -*- coding: UTF-8 –*-
__author__ = 'Mr.Li'
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
import smtplib
import unittest
import time
import os

# ===================== Define the sending ================ 
DEF send_mail (file_new):
    f = open(file_new,'rb')
    mail_bobd = f.read()
    f.close()

    msg = MIMEText(mail_bobd,'html','utf-8')
    MSG [ ' the Subject ' ] = Header ( ' automatic test report ' , ' UTF-. 8 ' )

    smtp = smtplib.SMTP()
    smtp.connect("smtp.qq.com")
    smtp.login('[email protected]','mbnzfxlnmwbkbcfb')
    smtp.sendmail('[email protected]','[email protected]',msg.as_string())
    smtp.quit()
    print('email has send out!')

# =========== find test reports directory, find the latest generation of test reports file ============ 
DEF new_report (TestReport):
    lists = os.listdir(testreport)
    lists.sort(key=lambda fn: os.path.getmtime(testreport + '\\' + fn))
    file_new = os.path.join(testreport,lists[-1])
    print(file_new)
    return file_new

# Performing Test Method class 
IF  the __name__ == ' __main__ ' :
    test_report = 'C:\\Users\\86136\\PycharmProjects\\spider\\test\\test_project\\report\\'
    test_dir = 'C:\\Users\\86136\\PycharmProjects\\spider\\test\\test_project\\test_case'

    # The test suite is loaded into a 
    dicscover = unittest.defaultTestLoader.discover (test_dir, pattern = ' Test * .py ' )

    # Get the current time in a format 
    now The time.strftime = ( " % D%% Y-M-% H_% of M_% S " )

    # Storage path defined report 
    filename = test_report + ' \\ ' + + now ' and result.html ' 
    FP = Open (filename, ' WB ' )
     # define test report 
    Runner = HTMLTestRunner (Stream = FP,
                            title = ' test report ' ,
                            Description = ' implementation use cases: ' )
    runner.run (dicscover)   # run a test case 
    fp.close ()   # close the report file 

    new_report = new_report (test_report) # find the latest test report 
    send_mail (new_report) # Test Reports

 

Execution of the entire program can be divided into three steps:

1, by unittest framework () discover find matching test case by HTMLTestRunner the run () method to execute test cases and the latest generation of test reports.

2, call new_report () function to find the directory test report ( Report ) under the latest generation of test reports, test reports of the return path.

3, will be the latest test report path passed send_mail () function to achieve e-mail function.

The entire script execution is completed, open the recipient's mailbox, you can see the test report the latest test execution, as shown below:

 

 

Guess you like

Origin www.cnblogs.com/lirongyang/p/11595866.html