python之discover----基础运用

目录架构:case文件夹存放执行用例,log文件夹存放生成的测试报告
controller.py与case文件夹平级目录
discover 批量加载文件夹用例(参数说明)
- 参数:case_dir: 待执行用例的目录。
- 参数:pattern:这个是匹配脚本名称的规则,test*.py意思是匹配test开头的所有脚本。
- 参数:top_level_dir:这个是顶层目录的名称,一般默认等于None就行了
很香的源码
def load_all_case(): print(os.getcwd()) case_path = os.path.join(os.getcwd(), "case") //获取当前目录,拼接case目录 print(case_path) discover = unittest.defaultTestLoader.discover(case_path, pattern="*Case*.py", top_level_dir=None) print(discover) return discover if __name__ == '__main__': # runner = unittest.TextTestRunner() # runner.run(load_all_case()) file_prefix = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()) print(file_prefix) # 获取到当前文件的目录,并检查是否有 directory_time 文件夹,如果不存在则自动新建 directory_time 文件 try: File_Path = '.\\' + log + '\\' + directory_time + '\\' print(File_Path) if not os.path.exists(File_Path): os.makedirs(File_Path) print("目录新建成功:%s" % File_Path) else: print("目录已存在!!!") except BaseException as msg: print("新建目录失败:%s" % msg) fp = open('.\\' + log + '\\' + directory_time + '\\' + file_prefix + "xx系统测试报告.html", "wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'xx系统测试报告', description=u'测试用例执行情况') runner.run(load_all_case()) fp.close() # sender发送真 receiver接受者 auth_codes授权码 sender = 'xxx@163.com' //这里填写自己使用的发送,接收邮箱即可 receiver = 'xxxx.com' auth_code = 'xxxx' subject = 'his测试报告A' # 打开文件 读取并复制mail_body with open('.\\' + log + '\\' + directory_time + '\\' + file_prefix + "xx系统测试报告.html", 'rb') as f: mail_body = f.read() # 发送html格式脚本 html = MIMEText(mail_body, _subtype="html", _charset="utf-8") html["Subject"] = subject html["from"] = sender html["to"] = receiver # 添加附录文本 att1 = MIMEText(mail_body, "base64", "gb2312") att1["Content-Type"] = 'application/octet-stream' # 这里的filename不能写中文,附件的名字 att1["Content-Disposition"] = 'attachment; filename="His_Mz.html"' msg = MIMEMultipart() msg['Subject'] = subject # 邮件的标题 msg["from"] = sender msg["to"] = receiver msg.attach(html) # 将html附加在msg里 msg.attach(att1) # 新增一个附件 try: smtp = smtplib.SMTP() smtp.connect("smtp.163.com") smtp.login(sender, auth_code) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() print('发送邮件成功') except BaseException as msg: print("邮件发送失败", msg)
浙公网安备 33010602011771号