run.py

import os
import time
import unittest

from BeautifulReport import BeautifulReport

from Common.Log_common import LOG
from Common.login import change_user

from Common.patch import _TestCase


#####1.定义一个方法,功能是根据caselist.txt的路径,来获取要运行的用例名称列表

#path_caselist=os.path.join(os.path.dirname(__file__),'caselist.txt')


def get_case_name(path_caselist):
casename_list=[]
with open(path_caselist,encoding='utf-8')as f:
for value in f.readlines():
every_casename=str(value)#每一行的值,每一个值的后面会有一个换行f
if every_casename !=''and not every_casename.startswith("#"):#这一步是来屏蔽不想执行的用例名称
casename_list.append(every_casename.replace("\n", ""))
#用空代替每一个用例名称后面的换行符,即去掉\n,同时caselist中没有写后缀,加上后缀
f.close()
return casename_list
path_caselist = os.path.join(os.path.dirname(__file__), 'caselist.txt')
# print(get_case_name(path_caselist))

###2.再定义一个方法,功能是根据用例的目录,和选择执行用例的那个txt文件路径,来构造测试套件
#path_Testcases=os.path.join(os.path.dirname(__file__),'Testcases')#所有用例的目录

def get_test_suite(path_Testcases,path_caselist):
test_suite = unittest.TestSuite()
for case_name in get_case_name(path_caselist):
path_every_testcase=os.path.join(path_Testcases,case_name+'.py')
# print(path_every_testcase)
LOG.info('%s已被选择执行...'%path_every_testcase)

discover=unittest.defaultTestLoader.discover(path_Testcases,pattern=case_name+'.py',top_level_dir=None)
#discover()方法会自动根据测试目录匹配查找测试用例文件, 并将查找到的测试用例组装到测试套件中,这里的case_name我已经加了.py

for case in discover:
test_suite.addTest(case)
return test_suite



#######3.最后定义一个方法,根据用例的目录和txt文件的路径来运行用例,并且在相应的路径下按照时间戳命名生成报告

#path_report = os.path.join(os.path.dirname(__file__), 'Report')
def runing(path_Testcases,path_caselist,path_report,description,detaily):
report_name='BeautifulReport_'+time.strftime("%Y%m%d_%H%M%S", time.localtime(time.time()))+'.html'

try:
test_suit=get_test_suite(path_Testcases,path_caselist)
if test_suit is not None:
LOG.info('********TEST START********')
run=BeautifulReport(test_suit)
run.report(description=description,tester=detaily,filename=report_name,log_path=path_report)
else:
LOG.info('加载测试套件失败,或者没有用例执行')
pass
except Exception as e:
LOG.info(str(e))
finally:
LOG.info('********TEST END********')


if __name__=='__main__':
unittest.TestCase = _TestCase
path_caselist = os.path.join(os.path.dirname(__file__), 'caselist.txt')
path_Testcases = os.path.join(os.path.dirname(__file__), 'Testcases')
path_report = os.path.join(os.path.dirname(__file__), 'Report')
runing(path_Testcases, path_caselist, path_report, '输入报告描述', '输入报告详细')

change_user()
# i=0
# while i<=5:#5个用户
# runing(path_Testcases, path_caselist, path_report,'输入报告描述','输入报告详细')
# # change_user()
# i=i+1



posted @ 2022-03-11 12:26  我的博客16000  阅读(111)  评论(0编辑  收藏  举报