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


Posted in Python onMarch 12, 2016

通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数、业务函数需要修改,那么势必要找出那些引用过这个被修改函数的地方,有些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" : "完成用例数", 
    "times" : -1, 
    "match" : "include", 
  },   
  { 
    "key" : "File.expand_path", 
    "display" : "有状态行数", 
    "times" : -1, 
    "ignore_case" : True, 
  },   
  { 
    "key" : "def\s+test_", 
    "display" : "有效用例数", 
    "times" : -1, 
    "match" : "regex", 
    "ignore_case" : True, 
  },   
  { 
    "key" : "#def\s+test_", 
    "display" : "注释用例数", 
    "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)  ##默认为不限制检查次数    
  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

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

Python 相关文章推荐
Python re模块介绍
Nov 30 Python
Python函数中*args和**kwargs来传递变长参数的用法
Jan 26 Python
Python实现确认字符串是否包含指定字符串的实例
May 02 Python
python实现跨excel的工作表sheet之间的复制方法
May 03 Python
使用python实现快速搭建简易的FTP服务器
Sep 12 Python
python实现银联支付和支付宝支付接入
May 07 Python
使用Python正则表达式操作文本数据的方法
May 14 Python
python SVM 线性分类模型的实现
Jul 19 Python
Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)
Feb 07 Python
浅谈Keras参数 input_shape、input_dim和input_length用法
Jun 29 Python
Elasticsearch py客户端库安装及使用方法解析
Sep 14 Python
python 通过exifread读取照片信息
Dec 24 Python
使用Python内置的模块与函数进行不同进制的数的转换
Mar 12 #Python
Python语言的面相对象编程方式初步学习
Mar 12 #Python
举例讲解Python中的list列表数据结构用法
Mar 12 #Python
Python中的if、else、elif语句用法简明讲解
Mar 11 #Python
使用Python读写文本文件及编写简单的文本编辑器
Mar 11 #Python
简单讲解Python中的数字类型及基本的数学计算
Mar 11 #Python
详解Python中的变量及其命名和打印
Mar 11 #Python
You might like
php扩展ZF――Validate扩展
2008/01/10 PHP
探讨php中防止SQL注入最好的方法是什么
2013/06/10 PHP
php 截取GBK文档某个位置开始的n个字符方法
2017/03/08 PHP
CodeIgniter整合Smarty的方法详解
2017/08/25 PHP
laravel 解决多库下的DB::transaction()事务失效问题
2019/10/21 PHP
PHP实现常用排序算法的方法
2020/02/05 PHP
jquery中的常用事件bind、hover、toggle等示例介绍
2014/07/21 Javascript
JS日期加减,日期运算代码
2015/11/05 Javascript
微信小程序日历组件calendar详解及实例
2017/06/08 Javascript
详解vue2.0监听属性的使用心得及搭配计算属性的使用
2018/07/18 Javascript
javacript replace 正则取字符串中的值并替换【推荐】
2018/09/13 Javascript
[10:24]郎朗助力完美“圣”典,天籁交织奏响序曲
2016/12/18 DOTA
python的即时标记项目练习笔记
2014/09/18 Python
Python中字符串的常见操作技巧总结
2016/07/28 Python
Python实现识别手写数字 简易图片存储管理系统
2018/01/29 Python
python  文件的基本操作 菜中菜功能的实例代码
2019/07/17 Python
Python格式化输出--%s,%d,%f的代码解析
2020/04/29 Python
python中JWT用户认证的实现
2020/05/18 Python
Docker如何部署Python项目的实现详解
2020/10/26 Python
用python批量下载apk
2020/12/29 Python
详解CSS3 弹性布局快速入门
2019/06/06 HTML / CSS
艺龙旅行网酒店预订:国内、港澳台酒店
2018/06/26 全球购物
ruby如何进行集成操作?Ruby能进行多重继承吗?
2013/10/16 面试题
中专自我鉴定范文
2013/10/16 职场文书
策划助理岗位职责
2013/11/18 职场文书
养殖行业的创业计划书
2014/01/05 职场文书
私人会所最新创业计划书范文
2014/03/24 职场文书
土地转让协议书
2014/04/15 职场文书
《灰椋鸟》教学反思
2014/04/27 职场文书
个人简历自荐信
2014/06/26 职场文书
群众路线自我剖析材料
2014/10/08 职场文书
十佳少年事迹材料
2014/12/25 职场文书
老公保证书
2015/01/17 职场文书
CSS 新特性 contain控制页面的重绘与重排问题
2021/04/30 HTML / CSS
Python字典的基础操作
2021/11/01 Python
Pyhton爬虫知识之正则表达式详解
2022/04/01 Python