How to count the keywords of a test script by writing a Python applet

Source: Internet
Author: User

How to count the keywords of a test script by writing a Python applet

Usually automated test project to a certain program, written test code will naturally be many, if the test script has been written very early now some basic functions, business functions need to be modified, it is necessary to identify those who have referred to the modified function, some IDES support full-text lookup and reference lookup, And some simple may not, because in the future to use statistical functions, and some other requirements, so wrote a script. In addition to the files referenced in full-text lookup under the directory, the number of statistics found is supported, one can find more than one keyword at a time, support by the primary keyword to categorize.
#encoding: Utf-8
Import OS
Import Sys
Import re

Reload (SYS)
Sys.setdefaultencoding ("Utf-8")

Short_exclude = [". SVN", "Sendbox"] # #不检查的文件, directory name
Long_exclude = [] # #不包含检查的文件, the full path to the directory
Extend_name = [". RB"] # #指定检查的文件后缀
Temp_key_words = [
{
"Key": "#",
"Display": "Author",
"Times":-1,
"Match": "include",
"Primary_key": True,
},
{
"Key": "#[summary]",
"Display": "Number of completed use cases",
"Times":-1,
"Match": "include",
},
{
"Key": "File.expand_path",
"Display": "Number of stateful rows",
"Times":-1,
"Ignore_case": True,
},
{
"Key": "Def\s+test_",
"Display": "Number of valid use cases",
"Times":-1,
"Match": "Regex",
"Ignore_case": True,
},
{
"Key": "#def \s+test_",
"Display": "Number of annotated use cases",
"Times":-1,
"Match": "Regex",
"Ignore_case": True,
},
]

For KV in Temp_key_words:
If not ' key ' in KV:
Raise "the" key "value is not in the list below! \n%s "% kv
If not ' key ' in KV:
Raise "display" value is not in the list below! \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: # #在不检查文件, directory Range
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: # #不在后缀名 Check Range
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: # #检查次数已满, no more checking

Continue
If Kv.get (' Match ', "") = = "Regex": # #指定了匹配方式为: Regular
If kv[' pattern '].match (line): # #匹配正则成功
Match = True
else: # #默认匹配方式为: Contains
If kv[' key '] in line: # #包含了指定字符串
Match = True
If match:
If Kv.get (' Primary_key ', False):
kv[' primary_key_value ' = line.split (kv[' key '). Strip ()
# kv[' primary_key ') = False
Kv[' Times ']-= 1 # #匹配成功, same as the number of remaining matches-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 ("File name:%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 ("=========================== primary key statistics split line ===============================")
Total_dict = {}
For KV in Key_words:
If ' Primary_key ' not in KV:
total_dict[kv[' Display '] = 0
total_dict[' Total number of files '] = 0
For k,v in P_k_dict.items ():
Temp_list = []
Temp_list.append ("PRIMARY key:%s\n Total Files:%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[' Total number of files '] + = v[0]
Tip_list.append ("=========================== all statistics split Line ===============================")
Temp_list = []
For k,v in Total_dict.items ():
Temp_list.append ("All%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 "The given root directory is invalid \n%s"% Root_dir
The settings at the beginning of the configuration can be used to determine what keywords to check, file types, which files and directories to filter, etc.

Previous page

How to count the keywords of a test script by writing a Python applet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.