程序结构:
1,首先连接数据库(mysql.py)并获取到数据库里面的数据
import pymysql # 创建链接数据库的方法 def lianSql(host, user, password, database): """host:数据库地址,user:登陆数据库用户名,password:登陆密码,database具体数据库名称""" MyDatabase = pymysql.connect(host=host, user=user, password=password, database=database) return MyDatabase # 根据数据库查询数据 def dbShuJu(): x = lianSql('localHost', '', '', 'z1r') # 创建数据库对象 cur = x.cursor() # 编写sql语句,z主要是查询 selectSql = 'select * from user' # 执行查询sql语句 cur.execute(selectSql) # 获取返回的所有数据 result = cur.fetchall() return result
2,根据pymysql发送带有测试报告的附件(html)到邮箱,如果想定时执行脚本可直接使用Windows任务计划执行Python测试脚本,或者是jenkens
import smtplib import time from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication """email_from:发送人,email_password:发送者的密码,email_to:邮件接收者,filename:报告地址,**kwargs:内容标题""" def sendMail(email_from, email_password, email_to, filename, other=None): # 创建一个可以同时添加正文和附件的msg message = MIMEMultipart() # 格式化获取当前系统时间 times = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) message['Subject'] = '这是%s发送的测试报告' % (times) message['From'] = email_from message['To'] = email_to # 添加正文,其中other可以作为可选参数 if other is None: other = "这是默认的邮件正文" else: other = other message.attach(MIMEText(other, _subtype='html', _charset='utf-8')) # 创建一个可添加附件的对象 fujian = MIMEApplication(open(filename, 'rb').read(), 'utf-8') # 添加邮件的标题 fujian.add_header('Content-Disposition', 'attachment', filename=r"测试报告.html") # 加载此附件 message.attach(fujian) # 发送邮件到qq邮箱 sends = smtplib.SMTP_SSL('smtp.qq.com', 465) # 登陆q邮箱 sends.login(email_from, email_password) sends.sendmail(email_from, email_to, message.as_string()) sends.quit() # print('发送成功')
3,获取数据库数据,或者是通过接口调用数据
import unittest import HTMLTestRunnerNew from lx2023.SendEmail import sendMail from lx2023.Mysql import dbShuJu import ddt import json import requests # 通过数据库获取数据 dbs = list(dbShuJu()) # print(dbs), fs = list(dbs) """fs的数据格式:[(1, 'z1r', '7022601c.com', 'z1'), (2, 'z1r3', '[email protected]', 'mimashisjsjj'), (3, 'kk', '123@123', 'dad')] """ # 通过接口调用获取数据 url = ''#接口请求地址 token = '11193aaa05564f68b7a2a11778c9379171' data = {"token": token, "current": 1, 'pageSize': 10}
#构造并发起请求 re = requests.get(url, params=data) result = json.loads(json.dumps(re.json(), ensure_ascii=False)) res1 = result["data"]["data"] # 声明了ddt类装饰器 @ddt.ddt() class Test1(unittest.TestCase): def setUp(self) -> None: self.x = 1 def test_01(self): a = 200 b = 100 self.assertEqual(a, b, msg="%s不等于%s" % (a, b)) print(a) def test_02(self): c = "zhang" d = "zhang" print(self.assertNotEqual(c, d, msg="%s是等于%s" % (c, d))) def test_03(self): e = "abc" f = "d" self.assertIn(f, e, msg="%s不在%s中" % (f, e)) def test_04(self): x = 1 self.assertEqual(x, 1, "断言成功") def test_05(self): self.assertEqual(self.x, 1, "断言成功") @unittest.skip('跳过测试用例6') def test_06(self): self.assertEqual(self.x, 1, "断言成功") # @unittest.expectedFailure def test_07(self): self.assertEqual(self.x, 2, "断言成功") @ddt.data(*fs) # @ddt.unpack def test_8(self, item): self.assertEqual('z1r', item[1], msg="成功") """其中数据类似@data([1, 2, 3], [4, 5, 6], [7, 8, 9]),执行后的结果 [1, 2, 3] [4, 5, 6] [7, 8, 9]""" @ddt.data(*res1) # 定义一个变量,存储 @ddt.data(*res1)里面的单个数据,传递列表变量数据 def test_9(self, resu2): #通过resu2传递的数据,既可以取resu2里面对应的值 self.assertEqual('ces', resu2['ownerNames'], msg='断言失败') def tearDown(self) -> None: print("用例执行完成") if __name__ == '__main__': # unittest.main() #构建测试suit suit = unittest.TestSuite() loder = unittest.TestLoader() suit.addTest(loder.loadTestsFromTestCase(Test1)) # 准备生成的测试报告 files = '4.html' f = open(files, 'wb') #根据测试case生成测试报告 runner = HTMLTestRunnerNew.HTMLTestRunner(stream=f, verbosity=34, description="测试报告") runner.run(suit) #将生成的报告发送到邮箱 sendMail('', '', '', files, '这是我输入的')
其中使用到的HTMLTestRunnerNew.py文件在另一博客里
标签:python,self,pymsql,ddt,test,import,email,def From: https://www.cnblogs.com/zhangchaorun/p/17204234.html