欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

編寫Python小程序來統(tǒng)計測試腳本的關(guān)鍵字

 更新時間:2016年03月12日 17:01:00   作者:像風(fēng)一樣的自由  
這篇文章主要介紹了編寫Python小程序來統(tǒng)計測試腳本的關(guān)鍵字的方法,文中的實例不僅可以統(tǒng)計關(guān)鍵字?jǐn)?shù)量,還可以按主關(guān)鍵字來歸類,需要的朋友可以參考下

通常自動化測試項目到了一定的程序,編寫的測試代碼自然就會很多,如果很早已經(jīng)編寫的測試腳本現(xiàn)在某些基礎(chǔ)函數(shù)、業(yè)務(wù)函數(shù)需要修改,那么勢必要找出那些引用過這個被修改函數(shù)的地方,有些IDE支持全文查找和引用查找,而有些簡單的可能就沒有,因為日后要用到統(tǒng)計功能、和一些其它的需求,所以寫了一個腳本。除了跟目錄下全文查找引用過的文件外,還是支持統(tǒng)計查找到的數(shù)量,一次可以查找多個關(guān)鍵字,支持按主關(guān)鍵字來歸類。

#encoding: utf-8 
import os 
import sys 
import re 
 
reload(sys) 
sys.setdefaultencoding("utf-8") 
 
short_exclude = [".svn", "sendbox"]  ##不檢查的文件、目錄名 
long_exclude = []  ##不包含檢查的文件、目錄的完整路徑 
extend_name = [".rb"] ##指定檢查的文件后綴 
temp_key_words = [  
  { 
    "key" : "#作者:", 
    "display" : "作者", 
    "times" : -1, 
    "match" : "include", 
    "primary_key" : True, 
  }, 
  { 
    "key" : "#[summary]", 
    "display" : "完成用例數(shù)", 
    "times" : -1, 
    "match" : "include", 
  },   
  { 
    "key" : "File.expand_path", 
    "display" : "有狀態(tài)行數(shù)", 
    "times" : -1, 
    "ignore_case" : True, 
  },   
  { 
    "key" : "def\s+test_", 
    "display" : "有效用例數(shù)", 
    "times" : -1, 
    "match" : "regex", 
    "ignore_case" : True, 
  },   
  { 
    "key" : "#def\s+test_", 
    "display" : "注釋用例數(shù)", 
    "times" : -1, 
    "match" : "regex", 
    "ignore_case" : True, 
  },   
] 
 
for kv in temp_key_words: 
  if not "key" in kv: 
    raise "以下的列表中沒有【key】值!\n%s" % kv 
  if not "key" in kv: 
    raise "以下的列表中沒有【display】值!\n%s" % kv   
  kv['times'] = kv.get('times', -1)  ##默認(rèn)為不限制檢查次數(shù)    
  if kv.get("ignore_case", True)==False: ##默認(rèn)忽略大小寫 
    flag = 0 
  else: 
    flag = re.I     
  kv['pattern'] = re.compile(kv['key'], flag) 
  if kv.get("primary_key", False): 
    kv['times'] = 1 
import copy 
key_words = []     
 
def deepcopy(objs): 
  t_list = [] 
  for obj in objs: 
    t_list.append(copy.copy(obj)) 
  return t_list 
 
def loop_case(root_dir): 
  t_sum = [] 
  print root_dir 
  sub_gen = os.listdir(root_dir) 
  for sub in sub_gen: 
    if sub in short_exclude: ##在不檢查文件、目錄范圍中 
      continue 
    abs_path = os.path.join(root_dir, sub) 
    if long_exclude: 
      is_exclude = False 
      for exclude in long_exclude: 
        if exclude == abs_path[-len(exclude):]: 
          is_exclude = True 
          break 
      if is_exclude: 
        continue 
    print abs_path 
    if os.path.isdir(abs_path): 
      print "dir" 
      t_sum.extend(loop_case(abs_path)) 
    elif os.path.isfile(abs_path):       
      if not "." + abs_path.rsplit(".", 1)[1] in extend_name: ##不在后綴名 檢查范圍中 
        continue 
      print "file" 
      global key_words  
      key_words = deepcopy(temp_key_words)      
      t_sum.append(count_case(abs_path))  
  return t_sum     
   
def count_case(abs_path):   
  t_dict = {} 
  with open(abs_path) as f: 
    for l in f: 
      l = l.strip() 
      match_rule(l)  
  index = 0 
  count_result = [0] * len(key_words)    
  for kv in key_words:  
    if 'primary_key' in kv: 
      t_dict['primary_key'] = kv.get('display') 
      t_dict['primary_key_value'] = kv.get('primary_key_value', "None") 
    count_result[index] = -1-kv['times']  
    index += 1  
  t_dict['match_result'] = count_result 
  t_dict['file_path'] = abs_path  
  return t_dict 
 
def match_rule(line): 
  primary_key = None  
  for kv in key_words: 
    match = False          
    if kv['times']==0: ##檢查次數(shù)已滿,不再檢查 
      continue 
    if kv.get('match', "") == "regex": ##指定了匹配方式為:正則 
      if kv['pattern'].match(line):  ##匹配正則成功 
        match = True 
    else:  ##默認(rèn)匹配方式為: 包含 
      if kv['key'] in line:  ##包含了指定字符串 
        match = True 
    if match: 
      if kv.get('primary_key', False): 
        kv['primary_key_value'] = line.split(kv['key'])[1].strip()   
#        kv['primary_key'] = False       
      kv['times'] -= 1      ##匹配成功,同理剩余匹配的次數(shù) -1 
  return primary_key     
   
def format_info(sum_list): 
  tip_list = []   
  p_k_dict = {} 
  for d in sum_list: 
    p_k = d['primary_key_value'] 
    if p_k not in p_k_dict: 
      p_k_dict[p_k] = [0] * len(key_words)  
    temp_list = [] 
    m = d['match_result'] 
    temp_list.append("文件名稱:%s\n%s:%s\n" % (d['file_path'], d['primary_key'], d['primary_key_value'])) 
    for i in range(len(m)): 
      if 'primary_key' in key_words[i]:         
        continue  
      else: 
        t_s = str(m[i]) 
      temp_list.append("%s:%s\n" % (key_words[i]["display"], t_s)) 
      p_k_dict[p_k][i] += m[i] 
    tip_list.append("".join(temp_list)) 
    p_k_dict[p_k][0] += 1 
  tip_list.append("===========================主鍵統(tǒng)計分割線===============================") 
  total_dict = {} 
  for kv in key_words: 
    if 'primary_key' not in kv: 
      total_dict[kv['display']] = 0 
  total_dict['全部文件數(shù)'] = 0 
  for k,v in p_k_dict.items(): 
    temp_list = [] 
    temp_list.append("主鍵:%s\n文件總數(shù):%s\n" % (k, v[0])) 
    for i in range(1, len(v)): 
      temp_list.append("%s:%s\n" % (key_words[i]["display"], str(v[i])))  
      total_dict[key_words[i]["display"]] += v[i]     
    tip_list.append("".join(temp_list)) 
    total_dict['全部文件數(shù)'] += v[0] 
  tip_list.append("===========================全部統(tǒng)計分割線===============================") 
  temp_list = [] 
  for k,v in total_dict.items(): 
    temp_list.append("全部%s:%s\n" % (k,v)) 
  tip_list.append("".join(temp_list)) 
  tip_msg = "\n".join(tip_list) 
  print tip_msg 
  open(r"sum_case.log", "w").write(tip_msg) 
   
if __name__=="__main__": 
  if len(sys.argv) > 1: 
    root_list = sys.argv[1:] 
  else: 
    root_list = [os.curdir] 
  sum_list = [] 
  for root_dir in root_list:   
    if os.path.exists(root_dir) and os.path.isdir(root_dir): 
      sum_list.extend(loop_case(root_dir)) 
      format_info(sum_list) 
    else: 
      print "給定的根目錄無效\n%s" % root_dir 

可以通過配置開頭的設(shè)置來確定檢查什么關(guān)鍵字,文件類型,過濾哪些文件和目錄等

相關(guān)文章

  • Python的Random庫的使用方法詳解

    Python的Random庫的使用方法詳解

    這篇文章主要介紹了Python的Random庫的使用方法詳解,random庫是使用隨機數(shù)的Python標(biāo)準(zhǔn)庫,python中用于生成偽隨機數(shù)的函數(shù)庫是random,需要的朋友可以參考下
    2023-07-07
  • allure結(jié)合python生成測試報告教程

    allure結(jié)合python生成測試報告教程

    這篇文章主要介紹了allure結(jié)合python生成測試報告教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python著名游戲?qū)崙?zhàn)之方塊連接 我的世界

    Python著名游戲?qū)崙?zhàn)之方塊連接 我的世界

    讀萬卷書不如行萬里路,學(xué)的扎不扎實要通過實戰(zhàn)才能看出來,本篇文章手把手帶你模仿著名游戲——我的世界,大家可以在過程中查缺補漏,看看自己掌握程度怎么樣
    2021-10-10
  • Python的deque雙端隊列詳解

    Python的deque雙端隊列詳解

    這篇文章主要介紹了Python的deque雙端隊列詳解,deque(雙端隊列)是一種數(shù)據(jù)結(jié)構(gòu),允許使用O(1)時間復(fù)雜度從兩端添加和刪除元素, Python的deque類實現(xiàn)了此數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下
    2023-09-09
  • elasticsearch python 查詢的兩種方法

    elasticsearch python 查詢的兩種方法

    這篇文章主要介紹了elasticsearch python 查詢的兩種方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • Python os.access()用法實例

    Python os.access()用法實例

    在本篇文章里小編給大家分享了關(guān)于Python os.access()用法實例內(nèi)容以及相關(guān)知識點,需要的朋友們學(xué)習(xí)下。
    2019-02-02
  • pycharm創(chuàng)建scrapy項目教程及遇到的坑解析

    pycharm創(chuàng)建scrapy項目教程及遇到的坑解析

    這篇文章主要介紹了pycharm創(chuàng)建scrapy項目教程及遇到的坑解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Python對中國500強排行榜數(shù)據(jù)進(jìn)行可視化分析實戰(zhàn)

    Python對中國500強排行榜數(shù)據(jù)進(jìn)行可視化分析實戰(zhàn)

    這篇文章主要介紹了Python對中國500強排行榜數(shù)據(jù)進(jìn)行可視化分析實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • python3中rsa加密算法詳情

    python3中rsa加密算法詳情

    這篇文章主要介紹了python3中rsa加密算法詳情,rsa加密,是一種加密算法,目前而言,加密算法,是對數(shù)據(jù)、密碼等進(jìn)行加密,下文更多相關(guān)介紹,需要的小伙伴可以參考一下
    2022-05-05
  • 編寫Python腳本來獲取mp3文件tag信息的教程

    編寫Python腳本來獲取mp3文件tag信息的教程

    這篇文章主要介紹了編寫Python腳本來獲取mp3文件tag信息的教程,代碼基于Python2.x,文中的注釋很詳細(xì),需要的朋友可以參考下
    2015-05-05

最新評論