51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1855|回复: 1
打印 上一主题 下一主题

Python+unittest自动化测试多线程执行合并测试报告

[复制链接]
  • TA的每日心情
    无聊
    12 小时前
  • 签到天数: 936 天

    连续签到: 3 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2020-8-6 09:31:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    主要解决思路就是利用 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插件下载地址:解压后放到工程目录即可

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-24 21:31 , Processed in 0.072051 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表