Keyword statistics program for python testing scripts

Source: Internet
Author: User
Tags writing test scripts

Usually the automated test project reaches a certain levelProgram, Written testCodeNaturally there will be a lot of tests. If some basic functions and business functions need to be modified after a long time of writing test scripts, it is necessary to find the places where the modified functions have been referenced, some ides support full-text search and reference search, but some are simple, because statistical functions and other requirements will be used in the future, so a script is written. In addition to searching for referenced files in the full text under the same directory, you can also count the number of searched files. Multiple keywords can be searched at a time and can be classified by primary keywords.

# Encoding: utf-8import osimport sysimport rereload (sys) sys. setdefaultencoding ("UTF-8") short_exclude = [". SVN "," sendbox "] # file not checked, directory name long_exclude = [] # Full path extend_name = [". RB "] # specify the file suffix for check temp_key_words = [{" key ":" # Author: "," 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 comment cases", "times":-1, "match ": "RegEx", "ignore_case": True,},] for Kv IN temp_key_words: If not "key" in KV: raise "The following list does not contain the key value! \ N % s "% kV if not" key "in KV: raise" is not displayed in the following list! \ N % s "% kV ['times '] = KV. get ('times ',-1) # The default value is if KV. get ("ignore_case", true) = false: # default case-insensitive flag = 0 else: Flag = Re. I kV ['pattern'] = Re. compile (Kv ['key'], flag) If KV. get ("primary_key", false): KV ['times '] = 1 import copykey_words = [] def deepcopy (objs): t_list = [] for OBJ in objs: t_list.append (copy. copy (OBJ) return t_listdef 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 = deepcopy (temp_key_words) t_sum.append (count_case (abs_path) not in the suffix Check range )) 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_dictdef match_rule (line): primary_key = none for Kv IN key_words: match = false if kV ['times '] = 0: # Check times are full. If KV is not checked again. get ('match', "") = "RegEx": # the matching method is: regular if kV ['pattern']. match (line): # match the regular expression successfully match = true else: # the default matching method is include if kV ['key'] In line: # contains the specified string 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 # matching successful, similarly, 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 segmentation line ================================ ") total_dict ={} for Kv IN key_words: If 'Primary _ key' not in KV: total_dict [kV ['display'] = 0 total_dict ['total files'] = 0 for K, V in p_k_dict.items (): temp_list = [] temp_list.append ("primary key: % s \ n total number of 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 files'] + = V [0] tip_list.append ("================ ===== all statistical split lines ================================= ") 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 specified root directory is invalid \ n % s" % root_dir

you can check the keyword, file type, and files and directories by setting the beginning of the Configuration.

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.