Use the HTMLTestRunner module to generate test report files in HTML format

1. Download HTMLTestRunner.py

HTMLTestRunner is an extension to the unittest module of the Python standard library. It generates easy-to-use HTML test reports. HTMLTestRunner is released under the BSD license.
Download address: http://tungwaiyip.info/software/HTMLTestRunner.html

Windows: Put the downloaded file into the Python34 installation directory...\Python34\Lib directory

2. Import the htmltestrunner module package

1  from HTMLTestRunner import HTMLTestRunner
 2  import unittest
 3  from time import strftime or import time,datetime
 4  import os
 5  from email.mime.text import MIMEText
 6  
7  from email.mime.multipart import MIMEMultipart #Mail   format 8 
9  
from  email.mime . image import MIMEImage #Send   emails with pictures

3. Define the test case path

test_dir = ' ./testcase/ '

or

list_1 = 'test_case\\testcase'

4. Find elements (testcase)

discover = unittest.defaultTestLoader.discover(test_dir,'*case.py')

5. Define the test report file name and add the previous time to the test report file name

# Define the report storage path, support relative path

file_name = './report/' + strftime('%Y_%m_%d_%H_%M_%S') + '.html'

6. Create the file

fp = open(file_name,'wb')
HTMLTestRunner(stream=fp,description= ' description ' ,title= ' title ' ).run(discover)

7. Close the file

fp.close()

Appendix:

# -*- coding: utf-8 -*-

import unittest
from HTMLTestRunner import HTMLTestRunner
import time,os,datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage



#Take all use case files in the test_case folder 
def creatsuitel(lists):
    testunit = unittest.TestSuite()
     # discover method definition 
    discover = unittest.defaultTestLoader.discover(lists, pattern= ' start_*.py ' , top_level_dir= None)
     # use cases filtered by the discover method are added to the test suite cyclically 
    for test_suite in discover:
         for test_case in test_suite:
            testunit.addTests(test_case)
            print (textsit)
     return textit
list_1 = 'test_case\\test_case_1'
alltestnames = creatsuitel(list_1)

#Add the previous time to the test report file name 
now = time.strftime( " %Y-%m-%M-%H_%M_%S " , time.localtime(time.time()))
filename = " report\\ " +now+ ' result.html '  #Define a report storage path, support relative path. 
fp = open(filename, ' wb ' )
runner = HTMLTestRunner(stream=fp, title='Report_title', description='Report_description')

if  __name__ == " __main__ " :
     #Execute test case set and generate report 
    runner = unittest.TextTestRunner()

 




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342703&siteId=291194637