在最后执行runall文件中出现以下问题:
源码:
import os
import common.HTMLTestRunner as HTMLTestRunner
import getpathInfo
import unittest
import readConfig
from common.configEmail import Mail # 引用configEmail文件mail函数
from apscheduler.schedulers.blocking import BlockingScheduler # 定时任务模块
import common.Log
send =Mail()
path = getpathInfo.get_Path()
report_path = os.path.join(path, 'result')
on_off = readConfig.ReadConfig().get_email('on_off')
log = common.Log.logger
class AllTest:#定义一个类AllTest
def init(self):#初始化一些参数和数据
global resultPath
resultPath = os.path.join(report_path, "report.html")#result/report.html
self.caseListFile = os.path.join(path, "caselist.txt")#配置执行哪些测试文件的配置文件路径
self.caseFile = os.path.join(path, "testCase")#真正的测试断言文件路径
self.caseList = []
def set_case_list(self):
"""
读取caselist.txt文件中的用例名称,并添加到caselist元素组
:return:
"""
fb = open(self.caseListFile)
for value in fb.readlines():
data = str(value)
if data != '' and not data.startswith("#"):# 如果data非空且不以#开头
self.caseList.append(data.replace("\n", ""))#读取每行数据会将换行转换为\n,去掉每行数据中的\n
fb.close()
def set_case_suite(self):
"""
:return:
"""
self.set_case_list()#通过set_case_list()拿到caselist元素组
test_suite = unittest.TestSuite()
suite_module = []
for case in self.caseList:#从caselist元素组中循环取出case
case_name = case.split("/")[-1]#通过split函数来将aaa/bbb分割字符串,-1取后面,0取前面
print(case_name+".py")#打印出取出来的名称
#批量加载用例,第一个参数为用例存放路径,第一个参数为路径文件名
discover = unittest.defaultTestLoader.discover(self.caseFile, pattern=case_name + '.py', top_level_dir=None)
suite_module.append(discover)#将discover存入suite_module元素组
print('suite_module:'+str(suite_module))
if len(suite_module) > 0:#判断suite_module元素组是否存在元素
for suite in suite_module:#如果存在,循环取出元素组内容,命名为suite
for test_name in suite:#从discover中取出test_name,使用addTest添加到测试集
test_suite.addTest(test_name)
else:
print('else:')
return None
return test_suite#返回测试集
def run(self):
"""
run test
:return:
"""
try:
suit = self.set_case_suite()#调用set_case_suite获取test_suite
print('try')
print(str(suit))
if suit is not None:#判断test_suite是否为空
print('if-suit')
fp = open(resultPath, 'wb')#打开result/20181108/report.html测试报告文件,如果不存在就创建
#调用HTMLTestRunner
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='Test Report', description='Test Description')
runner.run(suit)
else:
print("Have no case to test.")
except Exception as ex:
print(str(ex))
#log.info(str(ex))
finally:
print("*********TEST END*********")
#log.info("*********TEST END*********")
fp.close()
#判断邮件发送的开关
if on_off == 'on':
Mail().send()
else:
print("邮件发送开关配置关闭,请打开开关后可正常自动发送测试报告")
if name == 'main':
AllTest().run()
PS D:\interfaceTest> & d:/python3.8.1/python.exe d:/interfaceTest/runAll.py
test01case.py
suite_module:[<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<paramunittest.testUserLogin_0 testMethod=test01case>]>, <unittest.suite.TestSuite tests=[<paramunittest.testUserLogin_1 testMethod=test01case>]>, <unittest.suite.TestSuite tests=[<paramunittest.testUserLogin_2 testMethod=test01case>]>]>]>]
try
<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<paramunittest.testUserLogin_0 testMethod=test01case>]>, <unittest.suite.TestSuite tests=[<paramunittest.testUserLogin_1 testMethod=test01case>]>, <unittest.suite.TestSuite tests=[<paramunittest.testUserLogin_2 testMethod=test01case>]>]>]>
if-suit
EEE
Time Elapsed: 0:00:06.122190
TEST END
邮件发送成功
2、这套框架用flask在本地搭建服务,想请问,怎么把真实的项目接口及自己要写的测试用例运用到框架中。
最后:刚入python的小白,轻喷。
看下来用例维护在caselist.txt文件中