51Testing软件测试论坛

标题: Python+unittest自动化测试多线程执行合并测试报告 [打印本页]

作者: lsekfe    时间: 2020-8-6 09:31
标题: Python+unittest自动化测试多线程执行合并测试报告
主要解决思路就是利用 runner.run(all_case)执行结束后会返回测试结果,拿到测试结果后,再重新整合测试报告。
  同时利用threadpool.makeRequests(run_case, lst)构建多线程执行,但是这里多线程执行指的的是多个py文件里面的测试用例是并发的,但是单个py里面的测试用例执行还是按照字母顺序执行的。
  1. import datetime
  2.   import platform
  3.   import sys
  4.   import os
  5.   import time
  6.   import unittest
  7.   # 解决bat执行的路径问题
  8.   import threadpool as threadpool
  9.   from HTMLTestRunner import MergeResult, HTMLTestRunner
  10.   curPath = os.path.abspath(os.path.dirname(__file__))
  11.   rootPath = os.path.split(curPath)[0]
  12.   print(rootPath)
  13.   sys.path.append(rootPath)
  14.   current_path = os.getcwd()
  15.   current_path = r"E:\study"
  16.   allcase = r"E:\study\test_case"
  17.   if platform.platform().startswith("Linux"):
  18.   current_path = "M_site_selenium"
  19.   allcase = "M_site_selenium/case"
  20.   def create_suite():
  21.   # 不要太多其他的
  22.   discover = unittest.defaultTestLoader.discover(allcase, pattern='test*.py', top_level_dir=None)
  23.   return discover
  24.   # 所以case 结果result,存入list
  25.   all_result = []
  26.   def run_case(all_case, report_path, nth=0):
  27.   """执行所有的用例, 并把结果写入测试报告"""
  28.   retry_count = 2
  29.   fp_temp = open(report_path, "wb")
  30.   runner = HTMLTestRunner(stream=fp_temp, verbosity=3, retry=retry_count,
  31.   description=u'测试用例结果' + report_path)
  32.   # 调用add_case函数返回值
  33.   res = runner.run(all_case)
  34.   # 每个结果result 存在集合中,完事时候,集中输出html
  35.   all_result.append(res)
  36.   fp_temp.close()
  37.   if __name__ == "__main__":
  38.   # 按目录获取到的cases
  39.   allcasenames = create_suite()
  40.   # 保存的.html 结果目录
  41.   if not os.path.isdir(current_path + "/report/rp2"):
  42.   os.mkdir(current_path + "/report/rp2")
  43.   time_stamp = int(time.time())
  44.   count = 0
  45.   start_time = datetime.datetime.now()
  46.   # 3线程数
  47.   task_pool = threadpool.ThreadPool(3)
  48.   lst = []
  49.   for i, j in zip(allcasenames, range(len(list(allcasenames)))):
  50.   file_path = current_path + '/report/rp2/index' + str(time_stamp) + str(count) + ".html"
  51.   count += 1
  52.   # 禁止文件路转义
  53.   file_path = file_path.replace("\r", r"\r").replace('\n', r'\n')
  54.   print("文件路径:" + file_path)
  55.   # thread_pool 多参数写法
  56.   lst.append(([i, file_path, j], None))
  57.   rqs = threadpool.makeRequests(run_case, lst)
  58.   #  等待运行结束
  59.   [task_pool.putRequest(req) for req in rqs]
  60.   task_pool.wait()
  61.   end_time = datetime.datetime.now()
  62.   file_path_main = current_path + '/report/rp2/index' + str(time_stamp) + str(10000000) + ".html"
  63.   fp = open(file_path_main, "wb")
  64.   # 自定义生成html的class,目前是集合在HTMLTestRunner ,好像不能独立出来,用了很多HTMLTestRunner的自带方法
  65.   merge_html = MergeResult(fp=fp, result_list=all_result, start_time=start_time, end_time=end_time)
  66.   merge_html.make_html()
  67.   fp.close()
  68.   print(file_path_main)
  69.   print(len(all_result))
复制代码
HTMLTestRunner插件下载地址:解压后放到工程目录即可


作者: Miss_love    时间: 2020-12-30 14:37
支持分享




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2