Python利用pandas、request写自动化读取测试用例脚本

一、利用configparser读取server.conf文件的内容

server.conf:

[api_server] #节点,使用的接口服务器代号为api_server
ip=x.x.x.x  #服务器的地址
port=80                     #服务器访问的端口号

#数据库的信息

[db_server]
host=x.x.x.x
db=test
user=root
passwd=xxxxx

python 代码:

import configparser

# 读取server.conf文件内容
conf = configparser.ConfigParser()
conf.read('server.conf',encoding='utf-8')
which_server ='api_server'
ip = conf.get(which_server,'ip')
port = conf.get(which_server,'port')
base_url = f"http://{ip}:{port}"

二、读取数据库信息

# 获取数据库服务的信息
which_db = 'db_server'
host = conf.get(which_db,'host')
db = conf.get(which_db,'db')
user = conf.get(which_db,'user')
passwd = conf.get(which_db,'passwd')
db_info = {'host':host,'db':db,'user':user,'passwd':passwd}

三、初始化数据库

# 初始化数据库
conn = pymysql.connect(**db_info)
cursor = conn.cursor()
file = open('xxx.sql',encoding='utf-8') #sql文件是写好的SQL语句
for sql in file:
    if sql.strip() and sql.strip()[:2] !='--':
        cursor.execute(sql)
conn.commit()
conn.close()

四、读取测试用例

data = pandas.read_excel('api_login.xlsx',secols=['用例编号','用例标题','接口路径','请求方法','请求类型','参数','预期结果','验库表名','验库sql','数据库预期行数'])
case_info = data['用例编号']+'-'+data['用例标题']
url = 'http://192.168.224.131'+data['接口路径']
data.insert(0,'用例信息',case_info)
data.insert(1,'接口地址',url)
data.drop(['用例编号','用例标题','接口路径'],axis=1,inplace=True)
data['参数']=data['参数'].apply(eval)
data['预期结果']=data['预期结果'].apply(eval)
data['数据库预期行数']=data['数据库预期行数'].apply(eval)
cases = data.values.tolist()
for case in  cases:
    case_info,url,method,send_type,args,expect = case
    send = f"requests.{method}('{url}',{send_type}={args})"
    res = eval(send)
    actual = res.json()
    if actual == expect:
        print(f'{case_info}响应结果比对通过')
    else:
        print(f'{case_info}响应结果比对失败,预期结果:{expect},实际结果:{actual}')

最后代码:

import configparser,pymysql,pandas,requests
# 读取server.conf文件内容
conf = configparser.ConfigParser()
conf.read('server.conf',encoding='utf-8')
which_server ='api_server'
ip = conf.get(which_server,'ip')
port = conf.get(which_server,'port')
base_url = f"http://{ip}:{port}"
# 获取数据库服务的信息
which_db = 'db_server'
host = conf.get(which_db,'host')
db = conf.get(which_db,'db')
user = conf.get(which_db,'user')
passwd = conf.get(which_db,'passwd')
db_info = {'host':host,'db':db,'user':user,'passwd':passwd}
# 初始化数据库
conn = pymysql.connect(**db_info)
cursor = conn.cursor()
file = open('xxxx.sql',encoding='utf-8')
for sql in file:
    if sql.strip() and sql.strip()[:2] !='--':
        cursor.execute(sql)
conn.commit()
conn.close()
# 读取测试用例
data = pandas.read_excel('xxx.xlsx',secols=['用例编号','用例标题','接口路径','请求方法','请求类型','参数','预期结果','验库表名','验库sql','数据库预期行数'])
case_info = data['用例编号']+'-'+data['用例标题']
url = 'http://192.168.224.131'+data['接口路径']
data.insert(0,'用例信息',case_info)
data.insert(1,'接口地址',url)
data.drop(['用例编号','用例标题','接口路径'],axis=1,inplace=True)
data['参数']=data['参数'].apply(eval)
data['预期结果']=data['预期结果'].apply(eval)
data['数据库预期行数']=data['数据库预期行数'].apply(eval)
cases = data.values.tolist()
for case in  cases:
    case_info,url,method,send_type,args,expect = case
    send = f"requests.{method}('{url}',{send_type}={args})"
    res = eval(send)
    actual = res.json()
    if actual == expect:
        print(f'{case_info}响应结果比对通过')
    else:
        print(f'{case_info}响应结果比对失败,预期结果:{expect},实际结果:{actual}'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值