API自動化測試筆記(三):應用多線程+合並測試報告


(一)說明

    說明在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報告中即可。

 

 

代碼只截取了部分。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM