标签:
怎么样通过编写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" : "完成用例数",
"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) 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: ##检查次数已满,不再检查
标签:
原文地址:http://www.cnblogs.com/xdans/p/5412663.html