配置文件内容:
代码:
import unittest
import csv
import operator
from HTMLTestRunner import HTMLTestRunner
if __name__ == '__main__':
#打开对应的配置文件,进行读取
file=open("E:\interfacefram\config\config1.csv","r")
table=csv.reader(file)
dic={}
list=[]
line=0
for row in table:
line+=1
if line>1:
#把读取的数据放入字典
#每次循环前清空字典
dic={}
dic[row[1]]=row[0]
dic["num"]=int(row[3])
dic["state"]=row[2]
#print(dic)
list.append(dic)
list1=sorted(list,key=operator.itemgetter("num"))
#print(list1)
for i in range(0,line-1):
n=-1
for j in list1[i].items():
#print(j)
n+=1
if n==0:
fname=j[0]
route=j[1]
#print(route,fname)
if n==2:
state=j[1]
if state=="yes":
#print(fname)
#调用驱动
suite=unittest.defaultTestLoader.discover(route,pattern=fname)
#指定报告路径
path=(r"E:\interfacefram\report\mul_rep\test_report.html")
#打开并写入文件
file=open(path,"ab")
HTMLTestRunner(stream=file, verbosity=1, title="接口测试报告",tester="小张").run(suite)
file.close()
当print(dic)时打印出以下内容:
{'test_updataueser_v2.py': 'E:\\interfacefram\\script\\ind_interface', 'num': 3, 'state': 'yes'}
{'test_register.py': 'E:\\interfacefram\\script\\ind_interface', 'num': 1, 'state': 'yes'}
{'test_updatauser.py': 'E:\\interfacefram\\script\\ind_interface', 'num': 2, 'state': 'no'}
{'test_wjmm.py': 'E:\\interfacefram\\script\\mul_interface', 'num': 4, 'state': 'yes'}
然后把四个字典添加到列表list中,并以num为标准排序,得到list1。使用双重循环取出列表中的字典里的键和值:j,line为配置文件表格行数,输出结果如下:
('test_register.py', 'E:\\interfacefram\\script\\ind_interface')
('num', 1)
('state', 'yes')
('test_updatauser.py', 'E:\\interfacefram\\script\\ind_interface')
('num', 2)
('state', 'no')
('test_updataueser_v2.py', 'E:\\interfacefram\\script\\ind_interface')
('num', 3)
('state', 'yes')
('test_wjmm.py', 'E:\\interfacefram\\script\\mul_interface')
('num', 4)
('state', 'yes')
并在句中添加判断语句,当输出第一行时,把文件名和路径赋值给两个变量,当输出第三行时,把脚本状态赋值给一个变量。最后,当脚本状态为“yes”时,调用驱动程序执行测试,最后在指定路径生成报告文件。