前提:
1.全部的測試用例腳本均放在web_testcase文件夾下
2.生成測試報告的文件HTMLTestRunner_cn.py(轉自GitHub - GoverSky/HTMLTestRunner_cn: HTMLTestRunner 漢化版,同時支持python 2和3慨飘,增加截圖展示功能僚稿,失敗重試)
具體代碼如下:
# coding=utf-8
import unittest,os,time
import HTMLTestRunner_cn
import threading
import send_email
import sys
sys.path.append('..')
casepath = os.path.dirname(os.path.abspath('.'))+"/web_testcase"
if not os.path.exists(casepath):
? ? print("測試用例需放到‘case’文件目錄下")
? ? os.mkdir(casepath)
reportpath = os.path.dirname(os.path.abspath('.'))+"/result/report"
if not os.path.exists(reportpath): os.mkdir(reportpath)
# 確定測試用例以及測試報告所在文件夾
def allcase():
? ? discover = unittest.defaultTestLoader.discover(casepath,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pattern = "test*.py",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? top_level_dir= None)
? ? return discover
# 獲取全部測試用例文件
def runcase(suite):
? ? current_time = time.strftime("%Y-%m-%d-%H-%M")
? ? report_name =? current_time + "_Test_Result.html"
? ? htmlreportpath = os.path.join(reportpath,report_name)
? ? fp = open(htmlreportpath,"wb")
? ? proclist=[]
? ? s=0
? ? for i in suite:
? ? ? ? runner = HTMLTestRunner_cn.HTMLTestRunner(title="測試報告"+ str(s+1),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? description = '并發(fā)執(zhí)行測試用例:' ,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stream = fp,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? verbosity=2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? retry=1,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? save_last_try=True)
? ? ? ? proc = threading.Thread(target=runner.run,args=(i,))
? ? ? ? proclist.append(proc)
? ? ? ? s=s+1
? ? for proc in proclist:
? ? ? ? proc.start()
? ? for proc in proclist:
? ? ? ? proc.join()
? ? fp.close()
? ? send_email.send_email(htmlreportpath)
if __name__ == "__main__":
? ? runtmp = allcase()
? ? runcase(runtmp)
上述代碼,雖然能減少測試用例運行時間且成功的執(zhí)行并生成測試報告脂倦,但是仍有不足之處:
1.生成的測試報告頁面,每個測試用例文件均分開顯示(如果可以將全部測試)
2.命令行僅打印出了某個測試用例文件中測試用例的執(zhí)行結果,其他的均沒有在cmd窗口顯示測試結果(未知原因)