eval函数用来执行一个字符串表达式,并返回表达式的值,可以理解为剥离字符串外的引号,可用于将字符串转换为字典
自动执行excel登陆接口测试用例
初始化sql:sqls01.txt
-- 注册接口:test02、test03 delete from user where username = 'test02' delete from user where username = 'test03' insert into user(id,username,password) values(3,'test03','123456') --登陆接口:test01 delete from user where username = 'test01'; insert into user(id,username,password) values(2,'test01','123456')
用例:login.xlsx (data,expect)
case_name | data | expect |
---|---|---|
测试登陆成功 | {'username':'test01','password':'123456'} | 登录成功 |
测试用户名为空 | {'username':'','password':'123456'} | 用户名或密码为空 |
测试密码为空 | {'username':'test01','password':''} | 用户名或密码为空 |
测试用户名和密码为空 | {'username':'','password':''} | 用户名或密码为空 |
测试用户名错误 | {'username':'test001','password':'123456'} | 用户名或密码错误 |
测试密码错误 | {'username':'test01','password':'123'} | 用户名或密码错误 |
测试用户密码都错误 | {'username':'test001','password':'1236'} | 用户名或密码错误 |
import requests,pymysql,pandas try: #初始化数据库 sqlfile = open('sqls01.txt','r',encoding='utf-8')# 打开文件 conn = pymysql.connect(host='192.168.139.137',user='root',password='123456',db='exam')#连接或打开数据库 cursor = conn.cursor()# 创建游标 for row in sqlfile:# row表示一行 if len(row.strip())>0 and not row.startswith('--'):# 过滤掉空行和注释行 sql = row.strip()# 去掉首尾空白符,作为有效sql命令 cursor.execute(sql)# 执行sql语句 conn.commit()# 提交保存数据到数据库 提交事物 conn.close()#关闭数据库连接 sqlfile.close()#关闭文件 # 读取excel测试用例 # 读取excle中的指定列到内存excel对象中 excel = pandas.read_excel('login.xlsx',usecols=['data','expect']) cases = excel.values.tolist()#数据转为列表,包括用例数据和预期结果 # 接口地址 address = 'http://192.168.139.137/exam/login/' for case in cases:#case 表示某一行用例 argument = eval(case[0])#Excel中的data列,eval将字符串转为字典 expect = case[1]#Excel中的expect列 res = requests.post(url=address,data=argument) actual = res.text # 实际结果 # 比对响应结果正确性 if expect in actual: print('响应结果==验证通过') else: print('响应结果==验证失败==预期结果'+str(expect)+',实际结果'+str(actual)) except Exception as e: print(e)
1.初始化数据库 读取sql文件 遍历行 执行行中的sql命令 2.自动执行Excel用例 读取Excel文件 遍历行 发送请求 判断响应结果正确性 判断落库正确性
case_name | data | expect | expect_sql | expect_db_rows |
---|---|---|---|---|
软件注册成功 | {'username':'test02','password':'123456','confirm':'123456','name':'测试02'} | {'Status': 1000, 'Result': 'Success', 'Message': '注册成功'} | select count(*) from user where username='test02' | 1 |
测试用户名被占用 | {'username':'test03','password':'123456','confirm':'123456','name':'测试03'} | {'Status': 1003, 'Result': 'Username test03 is taken', 'Message': '用户名已被占用'} | select count(*) from user where username='test03' | 1 |
测试两个密码不一致 | {'username':'test04','password':'123456','confirm':'1234','name':'测试04'} | {'Status': 1002, 'Result': 'Password Not Compare', 'Message': '两次输入密码的不一致'} | select count(*) from user where username='test04' | 0 |
测试用户名为空 | {'username':'','password':'123456','confirm':'123456','name':'测试07'} | {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'} | select count(*) from user where username='' | 0 |
测试密码为空 | {'username':'test05','password':'','confirm':'123456','name':'测试05'} | {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'} | select count(*) from user where username='test05' | 0 |
测试确认密码为空 | {'username':'test06','password':'123456','confirm':'','name':'测试06'} | {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'} | select count(*) from user where username='test06' | 0 |
测试用户名密码确认密码均为空 | {'username':'','password':'','confirm':'','name':''} | {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'} | select count(*) from user where username='' | 0 |
import pymysql, requests, pandas # 初始化数据库 conn = pymysql.connect(host="192.168.139.137", user='root', password='123456', db='exam') cursor = conn.cursor() sqlfile = open('sqls01.txt', 'r', encoding='utf-8') # 工作中要将登陆 和 注册分开放入两个文件中 for row in sqlfile: if len(row.strip()) > 0 and not row.startswith('--'): sql = row.strip() cursor.execute(sql) conn.commit() conn.close() sqlfile.close() # 执行Excel用例 excel = pandas.read_excel('signup.xlsx', usecols=['data', 'expect', 'expect_sql', 'expect_db_rows']) cases = excel.values.tolist() address = 'http://192.168.139.137/exam/signup/' for row in cases: argument = eval(row[0]) # 要发送的参数 expect = eval(row[1]) # 预期响应结果 sql = row[2] # 预期sql语句 db_rows = row[3] # 预期数据库行数 res = requests.post(url=address, data=argument) actual = res.json() if actual == expect: print('响应结果==验证通过') else: print("响应结果==验证失败==预期结果\n\t" + str(expect) + ',实际结果\n\t' + str(actual)) # 落库检查 conn = pymysql.connect(host='192.168.139.137', user='root', password='123456', db='exam') cursor = conn.cursor() cursor.execute(sql) actual_rows = cursor.fetchone()[0] # fetchone是一个元祖,要第一个数据,即0号数据 if db_rows == actual_rows: print("落库检查通过") else: print("落库检查失败==预期数据库中存在:" + str(argument) + str(db_rows) + '行,实际存在' + str(actual_rows) + "行")