当前位置: 首页 > news >正文

2020-12-23

# -*- coding:utf-8 -*-
#__author__ = '大婶N72'
'''
定义导出数据库表数据到excel
1.init方法初始化获取配置文件数据
2.export2excel为主方法
'''
from xlrd import open_workbook
from xlutils.copy import copy
import os,logging,datetime
from public import config
from common import opmysql
operation_db=opmysql.OperationDbInterface(link_type=1)#实例化自动化测试数据库操作类
class AnalyseData(object):
    '''
    定义对接口测试数据分析的类,包含的方法有:
    1.导出测试数据到excel
    '''
    def __init__(self):
        self.field= config.field_excel#初始化配置文件
    #定义导出指定数据到excel表中
    def export2excel(self,names_export):
        '''
        :param names_export: 待导出的接口名称,列表形式数据
        :return:
        '''
        counts_export=len(names_export)#导出总数
        fail_export=[]#导出失败接口名列表
        try:
            src = open_workbook(config.src_path + '/report/report_module.xls',formatting_info=True)
            destination = copy(src)
            dt=datetime.datetime.now().strftime("%Y%m%d%H%M%S")#当前时间戳
            filepath=config.src_path+'/report/'+str(dt)+'.xls'
            destination.save(filepath)#保存模板表格到新的目录下
            for name_interface in names_export:
                cases_interface=operation_db.select_all("select * from case_interface where case_status=1 and name_interface='%s'"
                                                       %(name_interface))#获取指定接口的测试用例数据
                if len(cases_interface['data'])!=0 and cases_interface['code']=='0000':
                    src = open_workbook(filepath,formatting_info=True)
                    destination = copy(src)
                    sheet = destination.add_sheet(name_interface,cell_overwrite_ok=True)
                    for col in range(0,len(self.field)):
                        sheet.write(0,col,self.field[col])# 获取并写入数据段信息到sheet中
                    for row in range(1,len(cases_interface['data'])+1):
                        for col in range(0,len(self.field)):
                            sheet.write(row,col,'%s' %cases_interface['data'][row-1][col])#写数据到对应excel表中
                    destination.save(filepath)
                elif len(cases_interface['data'])==0 and cases_interface['code']=='0000':
                    fail_export.append(name_interface)
                else:
                    fail_export.append(name_interface)
            result={'code':'0000','message':'导出总数:%s,失败数:%s'
                                            %(counts_export,len(fail_export)),'data':fail_export}
        except Exception as error:#记录日志到log.txt文件
            result={'code':'9999','message':'导出过程异常|导出总数:%s,失败数:%s'
                                            %(counts_export,len(fail_export)),'data':fail_export}
            logging.basicConfig(filename = os.path.join(os.getcwd(), config.src_path + '/log/syserror.log'),level = logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
            logger = logging.getLogger(__name__)
            logger.exception(error)
        return result
if __name__ == "__main__":
    names_export=operation_db.select_one("select value_config from config_total where status=1 and key_config='name_export'")#获取导出的接口数据元祖
    if names_export['code']=='0000':#判断查询结果
        temp_export=eval(names_export['data'][0])#获取查询数据,并将其转换成字典
        test_analyse_data=AnalyseData()
        result_export=test_analyse_data.export2excel(temp_export)#导出结果
        print (result_export)
    else:
        print('获取导出接口集失败')




相关文章:

  • opmysql new
  • main
  • jmeter_(1)安装及配置
  • jmeter_(3)回放及断言
  • jmeter_(2)录制脚本
  • jmeter_汇总报告_数据解读
  • jmeter_接口间参数关联
  • python与selenium_环境搭建(1)
  • python与selenium_环境搭建(2)
  • python与selenium_基础篇_前端
  • python + selenium_元素定位
  • selenium_常用方法
  • selenium_其他方法
  • python与selenium_项目篇_项目实战、代码优化、项目重构
  • selenium+pytest自动化测试框架FAQ
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • Gradle 5.0 正式版发布
  • java2019面试题北京
  • Less 日常用法
  • Logstash 参考指南(目录)
  • pdf文件如何在线转换为jpg图片
  • session共享问题解决方案
  • Spring框架之我见(三)——IOC、AOP
  • Vim 折腾记
  • 笨办法学C 练习34:动态数组
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 前端之Sass/Scss实战笔记
  • 学习Vue.js的五个小例子
  • 你对linux中grep命令知道多少?
  • Linux权限管理(week1_day5)--技术流ken
  • UI设计初学者应该如何入门?
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • #图像处理
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (十一)图像的罗伯特梯度锐化
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (一) springboot详细介绍
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • (转)shell调试方法
  • .net Signalr 使用笔记
  • .NET牛人应该知道些什么(2):中级.NET开发人员
  • .NET企业级应用架构设计系列之开场白
  • .Net小白的大学四年,内含面经
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • [ 第一章] JavaScript 简史
  • [1] 平面(Plane)图形的生成算法
  • [1127]图形打印 sdutOJ
  • [20170713] 无法访问SQL Server
  • [Android]Android开发入门之HelloWorld
  • [BJDCTF 2020]easy_md5
  • [CakePHP] 在Controller中使用Helper
  • [FFmpeg学习]从视频中获取图片