测试工具python版

上一篇 / 下一篇  2021-08-10 10:30:49 / 个人分类:测试

index:
  1. 比较两列表元素(不考虑顺序,不考虑重复);
  2. 调get型数据库接口,循环造数据(示例);
  3. 连接操作mysql数据库;
  4. 多进程执行pytest UI脚本(示例).
1、比较两列表元素(不考虑顺序,不考虑重复)
def compareList(list1, list2):
    """比较列表元素,不考虑顺序,不考虑重复"""
    if sorted(set(list1)) == sorted(set(list2)):
        print('\n不考虑顺序,不考虑重复时,两个列表等价')
    else:
        a, b = [], []
        exist = 0
        for i in list1:
            for j in list2:
                if i == j:
                    exist = 1
                    break
                else:
                    exist = 0
            if exist == 0:
                a.append(i)
        for j in list2:
            for i in list1:
                if j == i:
                    exist = 1
                    break
                else:
                    exist = 0
            if exist == 0:
                b.append(j)
        print(f'\n两个列表不等价,列表1独特元素是:{a} \n两个列表不等价,列表2独特元素是:{b}')
效果:
2、调get型数据库接口,循环造数据(示例)
import requests,json,random
def send_get(url,params,headers={}):
    """get型接口调用"""
    response=requests.get(url=url,params=params,headers=headers)
    res_dict=json.loads(response.text)
    return res_dict
tasktype=['新建','活动中','已完成']
state=['取消','搁置','已完成']
url = 'https://xxx.yyy.zzz:18030/jdbcplat/update'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
for i in range(10001):
    sql=f'''
    insert into base_tasklist (id,tasktype,state) 
    values(10678+{i},{random.choices(tasktype)},{random.choice(state)})
    '''
    params={'server':'dlpt','sql':sql}
    print(send_get(url,params,headers))
3、连接操作mysql数据库
# coding=utf-8
# encoding: utf-8
"""
Author:刘源
Createtime:2021/08/08 14:25:42
Updatetime:2021/08/08 14:25:42
Description:
"""
import time, sys
# 上下文管理器,自动关闭文件,自动处理文件冲突
with open('tt21.txt', 'a') as f:
    f.write(f'{__file__}运行记录:')
    f.write(time.ctime())
    f.write('---' + str(time.time()) + '\n')
    print(sys.path)
# 连接mysql数据库
import MySQLdb
db = MySQLdb.connect(host="192.168.1.103", user="root",passwd= "Liuyuan3.14", db="tt1", charset='utf8',port=3306)
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()
print("Database version : %s " % data)
sql1='''select * from users'''
sql2="""INSERT INTO tt1.users (id, name, chinese, math, English, physics) 
VALUES (8, '冯锐', 77, 73, 93, 59);"""
# 使用execute方法执行SQL语句
cursor.execute(sql2)
db.commit() #增删改,需要提交;查询不用
cursor.execute(sql1)
# 使用 fetchone() 方法获取一条数据
data1 = cursor.fetchall()
for i in data1:
    print('select * from users',i)
# 关闭数据库连接
db.close()
效果:
注意,mysql连接失败原因可能有如下,可逐一排除:
Linux防火墙没关→systemctl stop firewalld,临时关闭它(不建议通过配置文件永久关闭)
②SeLinux模块的阻碍→setenforce 0,临时设置为带警告的强制执行模式(不建议通过配置文件永久关闭)
③服务器ip地址错误,尤其不是固定ip,又没域名映射、自动转换时→确定正确ip
④mysql没开启指定用户在指定ip的远程连接权限→自行设置哦
⑤Linux没有监听所有ip的连接请求→netstat -anop | grep -in mysql,自行验证下
⑥其他原因,比如系统升级,版本升级→试试重启,重启解决一半问题
4、多进程执行pytest UI脚本(示例)
效果:

TAG:

 

评分:0

我来说两句

Open Toolbar