框架驱动程序设计V3.0:按照指定顺序和状态来执行配置文件中的多个脚本,并输出HTML格式测试报告

配置文件内容:
在这里插入图片描述
代码:

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”时,调用驱动程序执行测试,最后在指定路径生成报告文件。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值