main.py 2.13 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os,time
import sys
import unittest
import re
import HTMLTestRunner_cn
from discover import DiscoveringTestLoader
from commons import SendEmail as em
from commons import common as com

def Create_Testcase_suite():
    '''创建测试套件'''
    testunit=unittest.TestSuite()
    test_Loader = DiscoveringTestLoader()
    discover=test_Loader.discover("./testcase/",pattern='test_*.py',top_level_dir=None)
    print(discover)
    for test_suite in discover:
        testunit.addTests(test_suite)
    print(testunit)
    return testunit

    
def Run_Testcase(testsuit):    
    '''运行测试用例并生成报告'''
    now = time.strftime("%Y-%m-%d %H_%M_%S",time.localtime())
    path=os.path.dirname(os.path.abspath(sys.argv[0]))
    report_file=path+"/report/"+now+"_result.html"
    #创建报告文件
    fp=open(report_file,'wb')

    runner=HTMLTestRunner_cn.HTMLTestRunner(
        stream=fp,
        title=u'搜索功能测试报告',
        description=u'用例执行情况:',
        verbosity = 2)
    #执行用例
    runner.run(testsuit)
    #关闭文件
    fp.close()
    return report_file
    
def Send_email(filename):
    '''判断邮件发送逻辑'''
    l=eval(com.get_global_config("global_data", "email","list").lower())
    if type(l)!=type([]):
        raise Exception("error,pls input list type send-email address")
    elif len(l)==0:
        print("\n list of send-email is null,won't send email!")
    elif len(l)!=0:
        for i in l:
            print(" check send-email format : {}".format(i))
            if re.match(r'^[0-9a-zA-Z_]{1,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$',i):
                pass
            else:
                raise Exception("error,pls check your send-email format")
        #发送邮件
        em.send_email(filename, l)
    else:
        print("\n Haven't sent the email,pls check send-email address!")
        
    

if __name__ == "__main__":
#   #创建测试套,运行测试用例,生成报告
    report=Run_Testcase(Create_Testcase_suite())
    #发送邮件
    Send_email(report)