本文讲述:测试过程中,通过自动化脚本实现毫秒上传批量附件。
注意:本文需要你有如下技能
笔者在测试过程中,统计到被测系统的各表单页面有共有39张附件(图片)需要上传;步骤是这样的:
那么,问题来了“我很生气…”
笔者尝试用自动化来上传附件,结果真的大吃一惊;
“本次总共上传了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()