#-*-coding:UTF-8-*-
import os
import chardet
#全局变量
FT_STANDARD_BEGIN = 'TEST_F'
g_allFtDic = {} #存放所有FT用例的字典, key是class+用例名,value是整个ft组成的字符串的hash值
g_repeatNumInOneCase = 0 #一个用例集内重复的FT用例个数
g_repeatNumInAllCases = 0 #所有的用例集重复的FT用例个数
g_allNum = 0 #所有的FT用例个数
g_repeatFtInfoInSameCase = []
g_repeatFtInfoInALLCases = []
g_am_dir_path = '/home/yh/code/amfVnfpV1/am/ft'
g_am_dbapp_dir_path = '/home/yh/code/amfVnfpV1/am/ft/dbapp_test'
g_messyCode_docu = []
g_test_path = '/home/yh/code/test/lanuage/python'
"""
代码库格式太多,统一格式很多文件打不开
以二进制方式读取文件,获取字节数据,检测编码格式,用于后续open接口使用
"""
def get_encoding(filename):
with open(filename, 'rb') as f:
return chardet.detect(f.read())['encoding']
"""
读取一个文件里所有的行,并且剔除特殊的行
"""
def getOneFtCaseLine(filePath, oneftCaseLines):
#print("enter getOneFtCaseLine")
fileEncoding = get_encoding(filePath)
try:
with open(filePath, encoding=fileEncoding) as fileMsg:
srcCaseLines = fileMsg.readlines()
for line in srcCaseLines:
if line.isspace(): #删除空白行
continue
line = line.strip() #删除首尾空格等
if line.startswith('//'): #删除//注释
continue
#还需要考虑删除掉/**/中间的所有内容
oneftCaseLines.append(line);
except:
g_messyCode_docu.append(filePath)
print("当前文件为:", filePath)
#print("当前共获取有效行数为:", len(oneftCaseLines))
"""
识别出一个用例集里的用例,插入到全局用例集和当前用例集,并进行判重处理
一个用例里有多对'{','}',首个'{'位置也不固定,需要特殊处理
"""
def saveEveryFtToDic(oneftCaseLines, oneFileFtDic, allCasesFtDic):
#print("enter saveEveryFtToDic")
oneFTSrcCodeStr = ''
leftBracesNum = 0
actualFtCode = False
for line in oneftCaseLines:
if line.startswith(FT_STANDARD_BEGIN):
value = line
actualFtCode = True
if line.endswith('{'): #针对{和用例名写到一行的情况
leftBracesNum += 1
oneFTSrcCodeStr = '{'
continue #用例名本身不放进去,不然必然不同
if actualFtCode:
if (line[0] == '{'):
leftBracesNum += 1
if (line[0] == '}'):
leftBracesNum -= 1
if (0 == leftBracesNum):
#print("当前用例的字符串是:", oneFTSrcCodeStr)
actualFtCode = False
global g_allNum
g_allNum += 1
key = hash(oneFTSrcCodeStr)
oneFTSrcCodeStr = ''
if (oneFileFtDic.__contains__(key)):
global g_repeatNumInOneCase
g_repeatNumInOneCase += 1
repeatStr = value + ' 与 ' + oneFileFtDic[key] + '冲突'
g_repeatFtInfoInSameCase.append(repeatStr)
else:
oneFileFtDic[key] = value
if (allCasesFtDic.__contains__(key)):
global g_repeatNumInAllCases
g_repeatNumInAllCases += 1
repeatStr = value + ' 与 ' + allCasesFtDic[key] + '冲突'
g_repeatFtInfoInALLCases.append(repeatStr)
else:
allCasesFtDic[key] = value
continue #不加continue,最后一个}会被统计到下一个用例
oneFTSrcCodeStr = oneFTSrcCodeStr + line
#print("单个用例集字典内容是:")
#print(oneFileFtDic)
def scanOneFtCase(filePath, allCasesFtDic):
oneftCaseLines = []
oneFileFtDic = {} #存放当前测试集下FT用例的字典, key是整个ft组成的字符串的hash值,value是用例名
getOneFtCaseLine(filePath, oneftCaseLines)
saveEveryFtToDic(oneftCaseLines, oneFileFtDic, allCasesFtDic)
def scanEachFtCase(dirPath):
allCasesFtDic = {} #存放所有测试集下FT用例的字典, key是整个ft组成的字符串的hash值,value是用例名oneFileFtDic = {}
for root,dirs,files in os.walk(dirPath):
#print ("current root: ", root)
#print ("current dirs: ", dirs)
for file in files:
# 使用join函数将文件名称和文件所在根目录连接起来
eachFilePtah = os.path.join(root, file)
if (eachFilePtah.endswith('.cc')):
scanOneFtCase(eachFilePtah, allCasesFtDic)
if __name__ == '__main__':
#scanEachFtCase(g_am_dir_path)
#scanEachFtCase(g_am_dbapp_dir_path)
scanEachFtCase(g_test_path)
print("同一个用例集内重复的FT用例个数:", g_repeatNumInOneCase)
print("所有用例集重复的FT用例个数:", g_repeatNumInAllCases)
print("所有的FT用例个数:", g_allNum)
print("同一个用例集内冲突的FT详细信息:", g_repeatFtInfoInSameCase)
print("所有用例集冲突的FT详细信息:", g_repeatFtInfoInALLCases)
print("乱码文件: ", g_messyCode_docu)