首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >API自动化测试笔记(三):应用多线程+合并测试报告

API自动化测试笔记(三):应用多线程+合并测试报告

作者头像
free赖权华
发布2020-05-04 15:01:20
5950
发布2020-05-04 15:01:20
举报
文章被收录于专栏:赖权华的笔记赖权华的笔记

(一)说明

    说明在API自动化如何使用多线程去执行测试用例,合并测试报告。

    不同测试框架有不同的地方,这里以unittest框架+BeautifulReport为例进行说明。

    步骤大概分为以下几步:

     1、获取所有测试套件。以[(测试套件1,0,),(测试套件, 1,).....]的格式返回

     2、应用concurrent.futures.ThreadPoolExecutor  将第一步的测试套件传给我们执行测试的方法,多线程并发执行用例,将测试报告放到临时目录下。

     3、将临时目录下的测试报告合并为一份测试报告

(二)获得测试套件、执行测试代码(仅供参考)

仅供参考,代码本身就不是完整的。

 1 #!/usr/bin/python
 2 # -*- coding: utf-8 -*-
 3 
 4 from unittest import defaultTestLoader
 5 from common.general import General
 6 from BeautifulReport import BeautifulReport
 7 import concurrent.futures
 8 
 9 
10 class RunTestTool():
11 
12     def __init__(self, temp_report_path, report_path, report_name="report_new.html"):
13         """
14         运行测试公共组件
15         :param temp_report_path: 测试报告临时存放目录(使用多线程生成的测试报告存放目录)
16         :param report_path: 测试报告路径
17         :param report_name: 测试报告文件名称
18         """
19         self.project_path = General.get_project_path()  # 获取工程路径
20         self.temp_report_path = '{}{}'.format(self.project_path, temp_report_path)
21 
22     def get_all_test_suit(self, test_case_path,description="测试报告", pattern='*_test.py'):
23         """
24         返回目录及子目录下的所有测试用例
25         :param description: 测试报告描述
26         :param test_case_path: 测试用例目录,接受list\tuple
27         :param pattern: 匹配测试用例文件的正则表达式
28         :return:
29         """
30         all_test_suit = []
31         test_suit = defaultTestLoader.discover(start_dir=test_case_path[0],
32                                                pattern=pattern,
33                                                top_level_dir=None)
34         if len(test_case_path) > 1:
35             for case_path in test_case_path[1:]:
36                 path = "{}{}".format(self.project_path, case_path)
37                 suit = defaultTestLoader.discover(start_dir=path,
38                                                   pattern=pattern,
39                                                   top_level_dir=path)
40                 test_suit.addTest(suit)
41         num = 0
42         for i in test_suit:
43             if len(list(i)) != 0:
44                 all_test_suit.append((i, num,description))
45                 num += 1
46         return all_test_suit
47 
48     def run_test(self, test_suit):
49         description = test_suit[2]
50         result = BeautifulReport(test_suit[0])
51         result.report(filename='report{}.html'.format(test_suit[1]),
52                       description=description,
53                       report_dir=self.temp_report_path)
54 
55 
56 test_case_path_list = ["目录1",'目录2']
57 temp_report_path = ""  # 测试报告临时目录
58 report_path = "" # 测试报告目录
59 rt = RunTestTool(temp_report_path=temp_report_path, report_path=report_path, )
60 test_suit = rt.get_all_test_suit(test_case_path=test_case_path_list,
61                                      description="描述11",
62                                      pattern="*_test.py")
63 with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:   # 多线程执行用例
64         executor.map(rt.run_test, test_suit)

(三)合并测试报告

     代码就不提供了,提供一个思路,会正则表达式应该很容易实现这个功能。

      1、看下BeautifulReport库生成的测试报告,会发现测试结果都放在这个变量里面 var resultData。

      2、 这样我们可以使用正则表达式将每一个临时测试报告中的resultData的值提取出来

       3、然后合并得到最终的结果,再序列化后替换进HTML报告中即可。

代码只截取了部分。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档