毫秒级上传批量附件

本文讲述:测试过程中,通过自动化脚本实现毫秒上传批量附件。

注意:本文需要你有如下技能

  • 测试基础知识
  • python编程知识
  • 数据库知识
  • 自动化框架思想

笔者在测试过程中,统计到被测系统的各表单页面有共有39张附件(图片)需要上传;步骤是这样的:

  • 1、表单页面,点击“上传”按钮,在页面弹窗中选择本地准备好的附件(图片),在熟练操作的情况下这里大约消耗了2秒;
  • 2、39张*2秒/张=72秒
  • 3、测试过程中,每天需要构造的测试数据,大约是30条
  • 4、把2和3相乘就是72秒/条*30条=2160秒,折合约36分钟,也就是仅仅上传附件就消耗了我36分钟;

那么,问题来了“我很生气…”

笔者尝试用自动化来上传附件,结果真的大吃一惊;

“本次总共上传了39张附件,共计耗时0.12213140000000003秒”,看到pycharm控制台打印的消息,笔者是非常开心。

[2088-08-8 8:8:8] - INFO: 连接数据库总耗时0.002419699999999997秒
......
[2088-08-8 8:8:9] - INFO: 本次总共上传了39张附件,共计耗时0.12213140000000003秒。

在这里插入图片描述

这么一算的话,39张*0.122秒/张=4.758秒;
由此看来,自动化速度大约是人工的454倍;

那么,笔者是怎么实现的呢?
不要急,站稳扶好,下面来撸码!
mysqlc.py

# -*- coding: utf-8 -*-
import pymysql.cursors
from common.myLog import *
from config.readConfig import *
import time
class mysql_do():
    def __init__(self,db):
        self.log = MyLog()
		self.connect = pymysql.Connect(
            # 读取测试环境的数据库连接信息
            host=self.dbinfo["host"],
            port=int(self.dbinfo["port"]),
            user=self.dbinfo["user"],
            passwd=self.dbinfo["passwd"],
            db= db,
            charset='utf8'
        )
        self.cursor = self.connect.cursor()
        time4 = time.perf_counter()
        self.log.info(message="连接数据库总耗时%s秒" % (time4 - time3))

    def sqlselect3(self):
        sqlselect1 = 'SELECT id from 表名1 ORDER BY id DESC LIMIT 1'
        sqlselect2 = 'SELECT id from 表名2  ORDER BY id DESC LIMIT 1'
        exe1= self.cursor.execute(sqlselect1)
        res = self.cursor.fetchall()
        exe2= self.cursor.execute(sqlselect2)
        res1 = self.cursor.fetchall()
        a_id = res[0][0]
        ab_id = res1[0][0]
        return a_id ,ab_id 

     def sqlselect3_insert(self,**kwargs):
        time3 = time.perf_counter()
        a= kwargs["a"]
        sql = mysql_do(db = '表名').sqlselect3()
       
        b_id = [1,2,3,.....,39]
        k = 0
        for k in range (0,len(b_id )):
            att_sql = b_id [k]
            att_id = sql[0] + k +1
            attachment_rel_id = sql[1] + k +1
            i1 = "INSERT INTO `库名`.`表名1`(`id`, `name`, `url`, `alias`, `remark`, `tmp`, `created_by`, `created_date`, `deleted`, `last_modified_by`, `last_modified_date`) VALUES ({}, 'a.png', 'http://www.***/filepath/a.png', NULL, NULL, '/data/path/5000-04/a.png', NULL, NULL, 0, NULL, NULL)".format(att_id)
            i2 = 'INSERT INTO `库名`.`表名2`(`id`, `data_id`, `at_id`, `at_type_id`, `remark`, `tmp`, `created_by`, `created_date`, `deleted`, `last_modified_by`, `last_modified_date`) VALUES ({}, {}, {}, {}, NULL, NULL, NULL, NULL, 0, NULL, NULL)'.format(atta_rel_id,a,att,att_sql)
            k +=k
            self.cursor.execute(i1)
            self.cursor.execute(i2)
            self.connect.commit()
            self.log.info(message="附件图片插入成功,请查看`库名`.`表1`表id={}、`库名`.`表2`表id={}".format(att_id,atta_rel_id) )
        time4 = time.perf_counter()
        self.log.info(message="本次总共上传了{}张附件,共计耗时{}秒。".format(len(b_id ),(time4 - time3)))
        self.connect.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值