完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
目录
1、缩进目录 2、完整目录 具体内容 1、case:test_qq_api.py 2、case:test_integer_arithmetic.py 3、run_all_case.py 报告展示 |
|
相关推荐
3个回答
|
|
|
|
|
|
具体内容
1 case: test_qq_api.py #coding:utf-8import requestsimport unittestclass TestQQ(unittest.TestCase): ''' 测试QQ接口 ''' # key - 正确 def test_qq_right_key(self): url = "http://japi.juhe.cn/qqevaluate/qq" par = { "key": "980bf619855953f6ebef9abe90d52712", "qq": "384056521" } r = requests.get(url, params=par) res = r.json() self.assertTrue(res["reason"] == "success") self.assertTrue(res["error_code"] == 0) self.assertTrue(res["result"] != []) # key - 错误 def test_qq_error_key(self): url = "http://japi.juhe.cn/qqevaluate/qq" par = { "key": "123456789", # 错误的key "qq": "1812436356" } r = requests.get(url, params=par) res = r.json() self.assertTrue(res["reason"] == "KEY ERROR!") self.assertTrue(res["error_code"] == 10001) self.assertTrue(res["result"] == [])if __name__ == "__main__": unittest.main() 2 case: test_integer_arithmetic.py #coding:utf-8import requestsimport unittestclass IntegerArithmeticTest(unittest.TestCase): # def setUp(self): # print("setUP start") # def tearDown(self): # print("tearDown start") # # @classmethod # def setUpClass(cls): # print("setUpClass") # @classmethod # def tearDownClass(cls): # print("tearDownClass") def testAdd(self): # print("testAdd strat") self.assertEqual((1 + 2), 5) self.assertEqual((0 + 1), 1) def testMultiply(self): # print("testMultiply strat") self.assertEqual((0 * 10), 0) self.assertEqual((5 * 6), 30) def testSubtraction(self): # print("testSubtraction strat") self.assertEqual((0 - 5), -5) self.assertEqual((1 - 5), -4)if __name__ == "__main__": unittest.main() 3 run_all_case.py # coding:utf-8import unittestimport osimport timefrom common import html_reportfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimport smtplibcur_path = os.path.dirname(os.path.realpath(__file__))def add_case(case_name = "case", rule = "test*.py"): '''第一步: 加载测试用例 ''' case_path = os.path.join(cur_path, case_name) # 如果不存在case文件夹,就创建 if not os.path.exists(case_path): os.mkdir(case_path) print("test case path:%s" %case_path) # 定义参数 - discover discover = unittest.defaultTestLoader.discover(case_path, pattern = rule, top_level_dir=None) print(discover) return discoverdef run_case(all_case, report_name = "report"): '''第二步: 执行用例''' now = time.strftime("%Y_%m_%d_%H_%M_%S") report_path = os.path.join(cur_path, report_name) # 如果不存在report文件夹,就创建 if not os.path.exists(report_path): os.mkdir(report_path) report_abspath = os.path.join(report_path, "report.html") print("report path: %s"%report_abspath) fp = open(report_abspath, "wb") runner = html_report.HTMLTestRunner(stream = fp, title = u"自动化测试用例报告", description = u"用例执行情况") # 调用add_case, 返回值 runner.run(all_case) fp.close()def get_report_file(report_path): '''第三步: 获取最新的测试报告 ''' lists = os.listdir(report_path) lists.sort(key = lambda fn: os.path.getmtime(os.path.join(report_path, fn))) print(u"最新测试报告: " + lists[-1]) # 找到最新报告 report_file = os.path.join(report_path, lists[-1]) return report_filedef send_mail(sender, psw, receiver, smtpserver, report_file, port): '''第四步: 发送最新测试报告''' with open(report_file, "rb") as f: mail_body = f.read() # 定义邮件内容 msg = MIMEMultipart() body = MIMEText(mail_body, _subtype="html", _charset="utf-8") msg['Subject'] = u"自动化测试报告" msg["from"] = sender if isinstance(receiver, list): msg["to"] = ",".join(receiver) else: msg["to"] = receiver msg.attach(body) # 添加邮件 att = MIMEText(open(report_file, "rb").read(), "base64", "utf-8") att["Content-Type"] = "application/octet-stream" att["Content-Disposition"] = 'attachment; filename="report.html" ' msg.attach(att) try: smtp = smtplib.SMTP() smtp.connect(smtpserver) # 连接服务器 smtp.login(sender, psw) except: smtp = smtplib.SMTP_SSL(smtpserver, port) smtp.login(sender, psw) # 登录 smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() print("Test report email has send out !") if __name__ == "__main__": # 1 加载用例 all_case = add_case() # 2 执行用例 run_case(all_case) # 3 获取报告 - 最新报告 report_path = os.path.join(cur_path, "report") report_file = get_report_file(report_path) # 4 邮箱配置 sender = "384056521135@qq.com" # 发送邮箱 psw = "tsrpkljzdvzrbifekk" # 授权码 smtp_server = "smtp.qq.com" # 邮箱服务器 port = 465 # 端口号 receiver = ["1812436356135@qq.com"] # 接受邮箱 # 5 发送邮件 send_mail(sender, psw, receiver, smtp_server, report_file, port) |
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
“0元购”智元灵犀X1机器人,软硬件全套图纸和代码全公开!资料免费下载!
3702 浏览 2 评论
1399 浏览 0 评论
【实操文档】在智能硬件的大模型语音交互流程中接入RAG知识库
6743 浏览 1 评论
防止AI大模型被黑客病毒入侵控制(原创)聆思大模型AI开发套件评测4
1092 浏览 0 评论
不可错过!人工神经网络算法、PID算法、Python人工智能学习等资料包分享(附源代码)
3413 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 15:40 , Processed in 1.232215 second(s), Total 76, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号