接口自动化测试用例分布统计
HTML格式的统计结果:
代码片段:
#!/usr/bin/env python
#-*- coding:UTF-8 -*-
######################################
## 作用: 测试用例数统计脚本 ##
## 日期: 2014-11-09 ##
## 版本: V1.0 ##
## 作者: Strayeagle ##
## 日期: 2015-04-01 ##
## 版本: V1.1 ##
######################################
import os
import sys
import re
import time
from pylab import *
import decimal
from common.stub.DataStub import DataStub
from common.util.PathUtil import getReportPath
#要处理的文件后缀名称,存入一个list中
exts = ['.py']
#所有测试用例文件列表
def all_case_files():
test_case_files = []
for root,dirs,files in os.walk(os.getcwd()):
for fileName in files:
#检查子目录
if fileName.endswith('.py') and not fileName.endswith('__.py'):
##完整的文件路径(绝对路径),并取lower
fname = (root + os.sep + fileName).lower()
test_case_files.append(fname)
return test_case_files
#读取文件,获取:4个空格开头, + def + 空格 + '(self):' 结尾的行记录的数量
def test_case_count(fname):
count = 0
for file_line in open(fname).xreadlines():
m = re.match(r'^(\s{4})def(\s+)(test*)', file_line)
if m is not None:
count += 1
return count
#所有测试用例总数
def all_case_counts(dir_tpye=None):
count=0
for root,dirs,files in os.walk(os.getcwd()):
for fileName in files:
#检查子目录
##完整的文件路径(绝对路径),并取lower
fname = (root + os.sep + fileName).lower()
#测试用例的总数
ext = fileName[fileName.rindex('.'):]
if dir_tpye == 'all':
try:
if(exts.index(ext) == 0):
c = test_case_count(fname)
count += c
except:
pass
elif dir_tpye == 'manager':
if '_01_manager' in fname:
try:
if(exts.index(ext) == 0):
c = test_case_count(fname)
count += c
except:
pass
elif dir_tpye == 'app':
if '_02_app' in fname:
try:
if(exts.index(ext) == 0):
c = test_case_count(fname)
count += c
except:
pass
return count
#获取各个用例分目录下对应的测试用例数,返回值为dict
def get_each_dir_case_count(dirList,test_case_files):
case_count_dict = {}
case_count_dict.clear()
for each_case_file in test_case_files:
for each_dir in dirList:
if each_dir in each_case_file:
count = test_case_count(each_case_file)
if case_count_dict.has_key(each_dir):
cur_count = case_count_dict[each_dir] + count