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

597 篇文章 17 订阅
100 篇文章 2 订阅

(一)说明

说明在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报告中即可。

在这里插入图片描述
代码只截取了部分。
在这里插入图片描述
在这里插入图片描述
点赞关注~持续分享,加入我们,642830685,免费领取最新软件测试大厂面试资料和Python自动化、接口、框架搭建学习资料!技术大牛解惑答疑,同行一起交流。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值