python實現測試指令碼的關鍵字統計程式

來源:互聯網
上載者:User

通常自動化測試專案到了一定的程式,編寫的測試代碼自然就會很多,如果很早已經編寫的測試指令碼現在某些基礎函數、業務函數需要修改,那麼勢必要找出那些引用過這個被修改函數的地方,有些IDE支援全文尋找和引用尋找,而有些簡單的可能就沒有,因為日後要用到統計功能、和一些其它的需求,所以寫了一個指令碼。除了跟目錄下全文尋找引用過的檔案外,還是支援統計尋找到的數量,一次可以尋找多個關鍵字,支援按主關鍵字來歸類。

#encoding: utf-8import osimport sysimport rereload(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'] = 1import 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             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_dictdef 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

可以通過配置開頭的設定來確定檢查什麼關鍵字,檔案類型,過濾哪些檔案和目錄等

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.