微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

编写Python小程序来统计测试脚本的关键字

通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数、业务函数需要修改,那么势必要找出那些引用过这个被修改函数的地方,有些IDE支持全文查找和引用查找,而有些简单的可能就没有,因为日后要用到统计功能、和一些其它的需求,所以写了一个脚本。除了跟目录下全文查找引用过的文件外,还是支持统计查找到的数量,一次可以查找多个关键字,支持按主关键字来归类。

#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" : "完成用例数",{ 
    "key" : "File.expand_path","display" : "有状态行数","ignore_case" : True,{ 
    "key" : "def\s+test_","display" : "有效用例数","match" : "regex",{ 
    "key" : "#def\s+test_","display" : "注释用例数",] 
 
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)  ##认为不限制检查次数    
  if kv.get("ignore_case",True)==False: ##认忽略大小写 
    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: ##检查次数已满,不再检查 
      continue 
    if kv.get('match',"") == "regex": ##指定了匹配方式为:正则 
      if kv['pattern'].match(line):  ##匹配正则成功 
        match = True 
    else:  ##认匹配方式为: 包含 
      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      ##匹配成功,同理剩余匹配的次数 -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("===========================主键统计分割线===============================") 
  total_dict = {} 
  for kv in key_words: 
    if 'primary_key' not in kv: 
      total_dict[kv['display']] = 0 
  total_dict['全部文件数'] = 0 
  for k,v in p_k_dict.items(): 
    temp_list = [] 
    temp_list.append("主键:%s\n文件总数:%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['全部文件数'] += v[0] 
  tip_list.append("===========================全部统计分割线===============================") 
  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 

可以通过配置开头的设置来确定检查什么关键字,文件类型,过滤哪些文件和目录等

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用爬虫利器 Playwright,轻松爬取抖查查数据 我们先分析登录的接口,其中 url 有一些非业务参数:ts、he、sign、secret。 然后根据这些参数作为关键词,定位到相关的 js 代码。 最后,逐步进行代码的跟踪,发现大部分的代码被混淆加密了。 花费了大半天,来还原这些混淆加密的代码
轻松爬取灰豚数据的抖音商品数据 调用两次登录接口实现模拟登录 我们分析登录接口,发现调用了两次不同的接口;而且,需要先调用 https://login.huitun.com/weChat/userLogin,然后再调用 https://dyapi.huitun.com/userLogin 接口。 登
成功绕过阿里无痕验证码,一键爬取飞瓜数据 飞瓜数据的登录接口,接入了阿里云的无痕验证码;通过接口方式模拟登录,难度比较高。所以,我们使用自动化的方式来实现模拟登录,并且获取到 cookie 数据。 [阿里无痕验证码] https://help.aliyun.com/document_detail/1
一文教你从零开始入门蝉妈妈数据爬取,成功逆向破解数据加密算法 通过接口进行模拟登录 我们先通过正常登录的方式,分析对应的登录接口。通过 F12 打开谷歌浏览器的调试面板,可以看到登录需要传递的一些参数;其中看到密码是被加密了。 不过我们通过经验可以大概猜测一下,应该是通过 md5 算法加密了。 接下
抽丝剥茧成功破解红人点集的签名加密算法 抽丝剥茧破解登录签名算法,成功实现模拟登录 headers = {} phone_num = "xxxx" password = "xxxx" md5_hash = hashlib.md5() md5_hash.upda
轻松绕过 Graphql 接口爬取有米有数的商品数据 有米有数数据的 API 接口,使用的是一种 API 查询语言 graphql。所有的 API 只有一个入口,具体的操作隐藏在请求数据体里面传输。 模拟登录,获取 sessionId 调用登录接口,进行模拟登录。 cookies = {} head
我最近重新拾起了计算机视觉,借助Python的opencv还有face_recognition库写了个简单的图像识别demo,额外定制了一些内容,原本想打包成exe然后发给朋友,不过在这当中遇到了许多小问题,都解决了,记录一下踩过的坑。 1、Pyinstaller打包过程当中出现warning,跟d
说到Pooling,相信学习过CNN的朋友们都不会感到陌生。Pooling在中文当中的意思是“池化”,在神经网络当中非常常见,通常用的比较多的一种是Max Pooling,具体操作如下图: 结合图像理解,相信你也会大概明白其中的本意。不过Pooling并不是只可以选取2x2的窗口大小,即便是3x3,
记得大一学Python的时候,有一个题目是判断一个数是否是复数。当时觉得比较复杂不好写,就琢磨了一个偷懒的好办法,用异常处理的手段便可以大大程度帮助你简短代码(偷懒)。以下是判断整数和复数的两段小代码: 相信看到这里,你也有所顿悟,能拓展出更多有意思的方法~
文章目录 3 直方图Histogramplot1. 基本直方图的绘制 Basic histogram2. 数据分布与密度信息显示 Control rug and density on seaborn histogram3. 带箱形图的直方图 Histogram with a boxplot on t