載入指定用例並傳送報告

2021-10-22 09:35:04 字數 4102 閱讀 4958

import  os

from mode.get_token import token

from report.htmltestrunner import htmltestrunner

import smtplib

from email.header import header

from email.mime.text import mimetext

from email.mime.multipart import mimemultipart

from log.mylog import logger,logger

import unittest

import datetime

current_path = os.path.dirname(__file__)

# pro_path是工程目錄,構造了乙個case_pth路徑

case_path = os.path.join(current_path + '/' + "testcase")

case_list =

# 不執行的檔案用例

not_case_list = [

'test01.py', #

]def modify_text(report_path):

case_result = report_path + "/result/" + 'case_result.txt'

f= open(case_result, "a+")

f.seek(0)

print('清空檔案')

print(case_result)

f.truncate() #清空檔案

f.close()

def send_email_by_qq(to,files,host):

sender_mail = '[email protected]'

sender_pass = '[email protected]'

# 設定總的郵件體物件,物件型別為mixed

msg_root = mimemultipart('mixed')

# 郵件新增的頭尾資訊等

msg_root['to'] = ";".join(to)

# 郵件的主題,顯示在接收郵件的預覽頁面

subject = '%s 自動化測試結果'%(host)

msg_root['subject'] = header(subject, 'utf-8')

# 構造文字內容

text_info = '您好!\n 請檢視附件內容'

text_sub = mimetext(text_info, 'plain', 'utf-8')

msg_root.attach(text_sub)

for file in files:

txt_file = open(file, 'rb').read()

txt = mimetext(txt_file, 'base64', 'utf-8')

txt.add_header('content-disposition', 'attachment', filename=file)

msg_root.attach(txt)

try:

sftp_obj =smtplib.smtp('smtp', 25)

sftp_obj.login(sender_mail, sender_pass)

sftp_obj.sendmail(sender_mail, to, msg_root.as_string())

sftp_obj.quit()

print('sendemail successful!')

except exception as e:

print('sendemail failed next is the reason')

print(e)

def get_filename(case_path):

# 使用os.walk方法遍歷得到所有檔名稱filename的列表集合

all_case_list =

for dirpath, dirname, filename in os.walk(case_path):

for file in filename:

# 此處注意,要想拿到所有的testcase必須在每個資料夾中有乙個__init__.py檔案引導,否則無法獲取。

# 判斷檔案以.py結尾且不以__開始,為去除__init.py檔案和.pyx字尾的檔案

if file.endswith(".py") and not file.startswith("__"):

# logger.info(file)

return all_case_list

def add_case_suit(case_list):

'''新增case'''

logger.info("需要執行這些用例:\n%s" % case_list)

i = 0

sum = 0

suit = unittest.testsuite()

for case in case_list:

i += 1

logger.info("%s %s" % (i, case))

discover = unittest.defaulttestloader.discover(start_dir=case_path, pattern=case)

logger.info("%s %s %s" % (i, case, discover.counttestcases()))

print("%s %s %s" % (i, case, discover.counttestcases()))

sum += discover.counttestcases()

suit.addtest(discover)

logger.info(sum)

print(sum)

return suit

def run_case_suit(suit,report,host):

'''執行所有的case'''

run = htmltestrunner(title="%s自動化測試報告"%(host),

description="%s測試結果"%(host),

stream=open(report, "wb"),

verbosity=2,

# retry=1

)logger.info(suit)

run.run(suit)

if __name__ == "__main__":

test_dir = current_path + '/'+'testcase/testattactpretectionipv6/'

report_path = current_path + '/report/'

host = token().host

report = report_path + 'testreport/' + datetime.datetime.now().strftime('%m-%d_%h.%m')+"自動化測試報告%s.html"%(host)

result = report_path + 'result/' + 'case_result.txt'

logfile = logger().logname

# 1. 清空之前的結果

modify_text(report_path=report_path )

# 2. 獲取所有的用例檔案

all_case_list = get_filename(case_path=test_dir)

# 3.新增需要執行的用例檔案

case_list = list(set(all_case_list) - set(not_case_list))

suit = add_case_suit(case_list=case_list)

# 4.執行所有的case

run_case_suit(suit=suit,report=report,host=host)

# 5.傳送郵件

send_email_by_qq(to=['[email protected]'

],files=[report,result,logfile],

host=host)

監控伺服器ssh登入,並傳送報警郵件

最近想監控下雲主機的ssh登入情況,所以開始寫ssh登入報警監控。實現方式並不難。在郵箱中選擇 設定 賬戶 在如下圖處開啟pop3 smtp服務,並生成授權碼。登入要進行ssh登入監控的伺服器,在 etc ssh建立 sshrc 檔案 bin bash 獲取登入者的使用者名稱 user user 獲...

日誌收集並傳送指定郵箱(二)

這裡面的核心思想 通過intent 來啟動 外部郵箱,並設定好檔案附件,具體核心 如下 intent email new intent android.content.intent.action send 使用android.content.intent.action send來啟動外部郵箱 ema...

單例模式懶載入併發

單例雖然沒有快取寫的那麼平凡,如果在getinstance方法上加sychonize會大大影響效能,單例的寫只有在第一使用時才會寫。使用讀寫鎖操作,基本上都上的讀鎖,對其他執行緒訪問沒有影響public class singleton public static singleton getinsta...