unittest框架(惨不忍睹低配版)

根据我上个随笔的unittest框架优化得来,虽然对于smtp模块还是有点迷糊,不过还是勉强搭建运行成功了,还是先上代码:

#login_test.py
import requests
class Login():
def test_login(self,username,password):
url = "http://localhost:8080r/login"
user_pwd = {}
user_pwd["mobilephone"] = username
user_pwd["pwd"] = password
result = requests.get(url, params=user_pwd)
list = (result.text).split(",")
return list[3].split(":")[1].split("}")[0] if __name__ == '__main__':
result = Login()
Return = result.test_login("18688773461","123456")
print(Return)
#data_test.py
from openpyxl import load_workbook
class Date_test():
@classmethod
def Date_test_1(cls):
wb = load_workbook("login_data.xlsx")
ws = wb.get_sheet_by_name("Sheet1")
list1 = []
for i in range(1, ws.max_row + 1):
list = []
for j in range(1, ws.max_column + 1):
str = ws.cell(i, j).value
list.append(str)
list1.append(list)
wb.close()
return list1
@staticmethod
def Email_Date():
username = “username”
password = “password”
To_Email = “To_Email ” # 可以是多个邮箱地址,用“,”分离开
return username,password,To_Email
#test_case.py
from login_test import Login
from ddt import ddt,unpack,data
from data_test import *
import unittest
@ddt
class Run_Test(unittest.TestCase):#继承unittest.TestCase
"""
读取excel文件和cfg文件中数据
:param xlsx:
:param ws:
:return:
"""
@data(*Date_test.Date_test_1())
@unpack
def test_run1(self,Username,Password,Text):
self.Result = Login()
Return = self.Result.test_login(Username,Password)
self.assertEqual(eval(Return),Text)
#测试用例
if __name__ == '__main__':
unittest.main()
#Email_console.py
import HTMLTestRunnerNew
from email.mime.text import MIMEText
from email.header import Header
import smtplib
import unittest
import os
from data_test import *
from email.mime.multipart import MIMEMultipart
class Email_Console():
def __init__(self):
self.username,self.password,self.To_Email= Date_test.Email_Date() def send_main(self,file_new):
"""
发送邮件方法
:param file_new:
:return:
"""
msg = MIMEMultipart("Emaik_TestText")
msg['Subject'] = Header('自动化测试报告', 'utf-8')
msg['From'] = self.username
msg['To'] = self.To_Email
with open(file_new , 'rb') as f:
mail_body = f.read()
msg.attach(MIMEText(mail_body, 'html', 'utf-8')) with open("result.html","rb") as f:
mail_attach = f.read()
att1 = MIMEText(mail_attach, 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="report_test.html"'
msg.attach(att1) try:
smtp = smtplib.SMTP()
smtp.connect("smtp.163.com",25)
smtp.login( self.username,self.password)
smtp.sendmail( self.username, self.To_Email.split(",") , msg.as_string())
smtp.quit()
except Exception as e:
print("Send Email Failed!!!")
raise e def new_report(self,testreport):
"""
生成并查找查找最新测试报告方法
:param testreport:
:return:
"""
# 生成测试用例
fp = open("result.html", 'wb')
runner = HTMLTestRunnerNew.HTMLTestRunner(stream=fp, title='2019年3月28日作业测试报告', description='所有测试情况',
tester="桂马")
discove = unittest.defaultTestLoader.discover(".", pattern="test_*.py")
runner.run(discove)
fp.close()
#查找测试用例
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 if __name__ == "__main__":
email_test = Email_Console()
file_path = email_test.new_report(os.getcwd()) #查找新生的报告
email_test.send_main(file_new = file_path) #调用发邮件模块

unittest框架(惨不忍睹低配版)

注意点:

    1.message[To]和message[From]虽然网上有的人也是直接上的汉字,但是不知道为啥我这边总报错,反而直接上邮件地址运行相当顺利

    2.附上附件的代码写了两次,别问为啥,问就是写一遍会报错,卡这个地方卡了许久,留着疑问待后续解决吧

    3.虽然说有些惨不忍睹,不过好歹再加上window的定时运行,然后多添加几行测试用例,一个简陋的不能再简陋的框架就算搭起来了

    4.邮件模块果然是块硬骨头,写完博客继续啃

备注:2019年4月1日更新,优化邮件模块

上一篇:如何完整卸载Mysql数据库


下一篇:Selenium2+python自动化30-引入unittest框架