作者: | 来源:互联网 | 2023-09-25 14:56
目录
一、创建txt文件
二、创建runAll.py文件
基于之前文章:
1、环境的搭建:https://blog.csdn.net/Mojitoice/article/details/108992622?spm=1001.2014.3001.5501
2、配置文件的创建,封装配置文件的读取方法:https://blog.csdn.net/Mojitoice/article/details/109031074
3、excel读取方法的封装:https://blog.csdn.net/Mojitoice/article/details/109203605?spm=1001.2014.3001.5501
4、url拼接方法的封装:https://blog.csdn.net/Mojitoice/article/details/114400695
5、封装get,post请求方法:https://blog.csdn.net/Mojitoice/article/details/114402767
6、进行mock的方法封装:https://blog.csdn.net/Mojitoice/article/details/114403473
7、操作数据库和日志方法封装:https://blog.csdn.net/Mojitoice/article/details/114404093
8、发送邮件方法的封装+HTMLTestRunnerCN:https://blog.csdn.net/Mojitoice/article/details/114533283
9、自动化测试case的编写:https://blog.csdn.net/Mojitoice/article/details/114535006
背景:
将上节中的编写的测试case,进行执行,生成测试报告并发送邮件。当自动化测试case过多的时候,有些case不用本次执行,对于这种情况应该咋搞呢?
所以需要创建一个文件,用来控制哪些case需要执行,哪些case不需要执行。
now,献上代码:
一、创建txt文件
/Users/dongyue/Documents/framework/testFile/caselist.txt
#case/testToken
#case/test_pic_goods
#case/testpicGoods
#case/testGetItemByProductId
case/testCategoriesList
根据每个的开头是否存在“#”进行判断,如果存在#的话,就不执行该case,如果不存在,则执行,此时就需要一个判断是否执行,并执行所有自动化case的文件
二、创建runAll.py文件
/Users/dongyue/Documents/framework/runAll.py
from getPath import GetPath
import os
from common.log import logger
import unittest
from common import HTMLTestRunnerCN
from testFile.readConfig import ReadConfig
from common.configEmail import SendEmailpath = GetPath().getPath() #/Users/dongyue/Documents/framework
#定义报告文件夹路径
report_path = os.path.join(path,"result/report")
readconfig = ReadConfig()class AllTest():def __init__(self): #定义初始化数据global result_path#定义文件路径,以及文件名result_path = os.path.join(report_path,"report.html") #/Users/dongyue/Documents/framework/result/report/report.htmllogger.info("result_path:%s"%result_path)#定义要执行的case文件的路径self.case_list_path = os.path.join(path,"testFile/caselist.txt") #/Users/dongyue/Documents/framework/testFile/caselist.txtlogger.info("case_list_path:%s"%self.case_list_path)#定义需要测试case的py文件路径self.case_file = os.path.join(path,"case") #/Users/dongyue/Documents/framework/caselogger.info("case_file:%s"%self.case_file)#定义要执行的case的py文件集合self.caselist = []def set_caselist(self):#读取caselist.txt文件,写入caselist列表file = open(self.case_list_path)str = file.readlines()for i in str:if i != "" and not i.startswith("#"):self.caselist.append(i.split('/')[1].replace("\n","")+".py") #按照/进行切割字符串取后面带py文件的,然后将\n替换为空,最后添加到caselist中logger.info("newcaselist:%s"%self.caselist)return self.caselistfile.close()def set_case_suit(self):# self.set_caselist() #获取上述caselist列表test_suite = unittest.TestSuite()suite_module = []for case in self.set_caselist():discover = unittest.defaultTestLoader.discover( self.case_file, pattern=case, top_level_dir=None) ##批量加载用例suite_module.append(discover)if len(suite_module)>0:for suite in suite_module:for test_name in suite:test_suite.addTest(test_name)else:logger.debug("测试套件集合为空")return Nonereturn test_suitedef send_email(self):subject = readconfig.get_email("subject")mail_host = readconfig.get_email("mail_host")mail_user = readconfig.get_email("mail_user")mail_pass = readconfig.get_email("mail_pass")sender = readconfig.get_email("sender")receivers_str = readconfig.get_email("receivers")for i in range(len(eval(receivers_str))):# print(eval(receivers_str)[i])data = open('/Users/dongyue/Documents/framework/result/report/report.html', 'rb').read()c = SendEmail(subject,mail_host,mail_user,mail_pass,sender,eval(receivers_str)[i],data)c.sendemail()def run(self):#执行测试集,并且生成报告try:suite = self.set_case_suit()if suite:fp = open(result_path, 'wb')runner = HTMLTestRunnerCN.HTMLTestRunner(stream=fp, title='自动化测试报告', description='接口自动化测试python + unittest')runner.run(suite)if readconfig.get_email("on_off") == "on":self.send_email()else:logger.debug("邮件开关没有打开")else:logger.debug("测试套件集合为空")except Exception as ex:logger.debug(ex)finally:logger.info("*********TEST END*********")fp.close()if __name__ == '__main__':AllTest().run()
执行该文件。结果如下
报告如下:
邮件如下:
log文件显示:
至此。。。终。。。后续如果需要优化,我就再更新