Python3 + requests + unittest接口测试
一、缘 起
笔者最近完成了基于Python3 + requests + unittest的接口测试脚本,故在此做一下记录,于己为复盘,于彼为学习和参考
二、思 路
接口测试无非三步:
- 首先,造数据 - Python3连接MySQL,插入测试数据
- 然后,发请求 - requests模块依次发请求
- 最后,校验返回值 - 基于unittest框架,assert主要的response值
三、说 明
脚本的工程名为zhtyInterfaceTest,整体结构如下图
- .idea为PyChram编辑器自动生成的,不用管
- common为通用的模块/方法集合,如接口加密方法、配置parameters方法、配置数据库方法、读Excel方法,等,一切你能想到的辅助测试的方法都可以放到这里
- testCase为测试测试用例集合,笔者用unittest框架、用discover方法添加测试用例,那么想按顺序执行测试用例,就必须对测试用例命名按照ASCII顺序,具体如下图
- testFile存放测试数据和测试用图,测试数据为Excel格式
- testResult为测试结果集合,基于unittest框架,运行完脚本直接生成HTML格式的测试报告
- config.ini为配置文件,配置需要测试的URL、数据库及SQL语句
- runAll.py为主程序
四、实 现
- 造测试数据,如果读者对Python3操作数据库不甚熟悉,可以参考笔者之前写的文章《Python3连接MySQL数据库及基本操作》
- ./zhtyInterfaceTest/common/mysqlCfg.py
#!/usr/bin/python
# coding=utf-8
from pymysql import connect
from pymysql.err import OperationalError
import os
import configparser as cparser
# ------------------读取配置文件及数据库配置----------------------
base_path = str(os.path.dirname(os.path.dirname(__file__)))
base_path = base_path.replace('\\', '/')
cfg_path = base_path + '/config.ini'
cf = cparser.ConfigParser()
cf.read(cfg_path, encoding='utf-8')
host = cf.get('MYSQL', 'host')
port = cf.get('MYSQL', 'port')
user = cf.get('MYSQL', 'user')
password = cf.get('MYSQL', 'password')
db_name = cf.get('MYSQL', 'db_name')
charset = cf.get('MYSQL', 'charset')
# -----------封装MySQL数据库基本操作-----------------
class DB:
def __init__(self):
try:
# 连接数据库
self.conn = connect(
host=host,
port=int(port),
user=user,
password=password,
db=db_name,
charset=charset
)
except OperationalError as e:
print('MySQL error %d: %s' % (e.args[0], e.args[1]))
else:
self.cursor = self.conn.cursor()
def execute_sql(self, command, sql):
"""
插入表数据
:param command:
:param sql:
:return:
"""
if command in ('SELECT', 'select'):
# 如果为查询指令
sql = sql.encode('utf-8')
try:
self.cursor.execute(sql)
result = self.cursor.fetchall()
return result
except Exception as e:
print(e)
finally:
self.conn.close()
elif command in ('delete', 'DELETE', 'update', 'UPDATE', 'insert', 'INSERT'):
# 如果为增删改
sql = sql.encode('utf-8')
try:
self.cursor.execute(sql)
self.conn.commit()
except Exception as e:
self.conn.rollback()
print(e)
finally:
self.conn.close()
else:
print('Command Error!')
if __name__ == '__main__':
# sel_sql = cf.get('SQL', 'SELECT')
# s = DB().execute_sql('select', sel_sql)
print(DB().execute_sql('select', cf.get('SQL', 'SELECT'))[0][0])
- ./zhtyInterfaceTest/runAll.py
#!/usr/bin/python
# coding=utf-8
from HTMLTestRunner import HTMLTestRunner
import configparser as cparser
from common.mysqlCfg import DB
import unittest
import time
import os
discover = unittest.defaultTestLoader.discover('./testCase/teacher', pattern='test*.py')
if __name__ == '__main__':
# ------------------读取配置文件----------------------
base_path = str(os.path.dirname(__file__))
base_path = base_path.replace('\\', '/')
cfg_path = base_path + '/config.ini'
cf = cparser.ConfigParser()
cf.read(cfg_path, encoding='utf-8')
# 初始化测试数据
ins_tb_user = cf.get('SQL', 'ins_tb_user')
ins_relation_class = cf.get('SQL', 'ins_relation_class')
ins_sport_data_01 = cf.get('SQL', 'ins_sport_data_01')
ins_sport_data_02 = cf.get('SQL', 'ins_sport_data_02')
ins_class_course_data = cf.get('SQL', 'ins_class_course_data')
# 执行SQL
DB().execute_sql('insert', ins_tb_user)
DB().execute_sql('insert', ins_relation_class)
DB().execute_sql('insert', ins_sport_data_01)
DB().execute_sql('insert', ins_sport_data_02)
DB().execute_sql('insert', ins_class_course_data)
~
......
~
【脚 本 说 明】
- 通过读取config.ini文件的数据库配置信息和需要执行的SQL语句,实现连接数据库以及执行SQL的方法
- 在主程序runAll.py执行测试用例前提前造好测试数据
- 发request请求
- ./zhtyInterfaceTest/reqMethod.py
#!/usr/bin/python
# coding=utf-8
import configparser as cparser
import requests
import os
# ------------------读取配置文件----------------------
base_path = str(os.path.dirname(os.path.dirname(__file__)))
base_path = base_path.replace('\\', '/')
cfg_path = base_path + '/config.ini'
cf = cparser.ConfigParser()
cf.read(cfg_path, encoding='utf-8')
class RequestMethod:
""" 定义请求类型 """
def __init__(self):
"""初始化参数"""
self.base_url = cf.get('URL', 'base_url')
self.data = {}
self.files = {}
def get(self, url, params):
"""
定义get方法请求
:return:
"""
test_url = self.base_url + url
try:
return requests.get(url=test_url, params=params, timeout=60)
except TimeoutError:
return print('%s get request timeout!' % url)
def post(self, url, params):
"""
定义post方法请求
:return:
"""
test_url = self.base_url + url
try:
return requests.post(url=test_url, data=params, timeout=60)
except TimeoutError:
return print('%s post request timeout!' % url)
def post_with_file(self, url, params, fp):
"""
定义post方法请求
:return:
"""
test_url = self.base_url + url
file = {
'head_img': open(fp, 'rb')
}
try:
return requests.post(url=test_url, data=params, files=file, timeout=60)
except TimeoutError:
return print('%s post request timeout!' % url)
- ./zhtyInterfaceTest/testCase/teacher/test_1_login.py
#!/usr/bin/python
# coding=utf-8
from common.reqMethod import RequestMethod
from common.getParams import *
import unittest
class Login(unittest.TestCase):
"""登录接口测试"""
def setUp(self):
self.url = 'login'
self.sheet = 'login'
def tearDown(self):
print(self.req_result)
def test_login_success(self):
"""
测试正常登录
:return:
"""
case = 'test_login_success'
# 获取请求参数
param = get_req_params(self.sheet, case)
# 发起请求
self.req_result = RequestMethod().post(self.url, param).json()
~
......
~
【脚 本 说 明】
- 读配置文件获取基础URL,然后依次定义三种常用的HTTP请求方法:get、post、post_with_file
- 根据请求方法的不同,传参的类型一般为:测试接口(与基础URL拼接为请求URL)、params和fp(上传文件的路径)
- 在每一个测试用例中,通过调用getParams.py模块的get_req_params方法,从Excel获取请求参数,通过get_resp_params方法从Excel获取响应参数
- 在测试用例中调用reqMethod.py中封装的请求方法,发接口请求
- 校验返回值
- ./zhtyInterfaceTest/testCase/teacher/test_1_login.py
from common.reqMethod import RequestMethod
from common. ......
class Login(unittest.TestCase):
......
def test_login_success(self):
......
# 返回参数断言
self.assertEqual(self.req_result['code'], get_resp_params(self.sheet, case, 'code'))
self.assertEqual(self.req_result['msg'], get_resp_params(self.sheet, case, 'msg'))
self.assertEqual(self.req_result['data']['user_name'], get_resp_params(self.sheet, case, 'user_name'))
......
【脚 本 说 明】
- 通过调用getParams.py模块的get_resp_params方法从Excel获取响应参数,和接口返回的参数做比对,实现断言
- 生成测试报告
- ./zhtyInterfaceTest/runAll.py
......
now = time.strftime('%Y-%m-%d %H_%M_%S') # 获取当前时间
filename = now + ' testReport.html'
fp = open('./testResult/{0}'.format(filename), 'wb')
runner = HTMLTestRunner(stream=fp,
title='智慧体育教师版接口测试报告',
description='用例执行情况:')
runner.run(discover)
fp.close()
......
五、最 后
运行结果
Git地址:https://gitee.com/freedomlidi/zhtyInterfaceTestPublic.git
OK!
~
~
~
不积跬步,无以至千里
Python3 + requests + unittest接口测试的更多相关文章
- python3+requests+unittest:接口自动化测试(一)
转载请表明出处:https://www.cnblogs.com/shapeL/p/9179484.html 简单介绍框架的实现逻辑,参考代码的git地址: https://github.com/zha ...
- python3 requests 进行接口测试、爬虫使用总结
Requests 是第三方模块,如果要使用的话需要导入.Requests也可以说是urllib模块的升级版,使用上更方便. 这是使用urllib的例子. import urllib.request i ...
- python3+requests:接口自动化测试(二)
转载请注明出处:https://www.cnblogs.com/shapeL/p/9188495.html 前言:上篇文章python3+requests+unittest:接口自动化测试(一):ht ...
- 对比3种接口测试的工具:jmeter+ant;postman;python的requests+unittest或requests+excel
这篇随笔主要是对比下笔者接触过的3种接口测试工具,从实际使用的角度来分析下3种工具各自的特点 分别为:jmeter.postman.python的requests+unittest或requests+ ...
- 【python3+request】python3+requests接口自动化测试框架实例详解教程
转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...
- python3+requests库框架设计01-自动化测试框架需要什么?
什么是自动化测试框架 关于自动化测试框架的定义有很多,在我大致理解下就是把能实现不同功能的软件组合在一起,实现特定的目的,这就是一个简单的自动化测试框架. 接口自动化测试框架核心无非是选择 一个用来编 ...
- requests,unittest——多接口用例,以及需要先登录再发报的用例
之前写过最简单的接口测试用例,本次利用unittest进行用例管理,并出测试报告,前两个用例是两个不同网页的get用例,第三个是需要登录才能访问的网页A,并在其基础上访问一个需要在A页面点开链接才能访 ...
- Python3+Requests+Excel完整接口自动化框架
框架整体使用Python3+Requests+Excel:包含对实时token的获取 框架结构图 1.------base -------runmethond.py runmethond:对不同的请求 ...
- requests+unittest+ddt+xlrd+pymysql+BeautifulReport数据驱动
# ddcapitestpython XXX接口自动化测试 # 一.数据驱动的思路 1.采用requests+unittest+ddt+xlrd+pymysql+BeautifulReport 2.r ...
随机推荐
- 爬虫时伪装header信息
在爬虫时,一般需要伪装Agent信息,放在header中 1.header不是必传参数,在需要的时候进行伪装 2.header = {"User-Agent": "Moz ...
- SpringBoot中Shiro使用Pac4j集成CAS认证
SpringBoot中Shiro使用Pac4j集成CAS认证 Pac4j 简介 Pac4j与Shiro,Spring Security一样都是权限框架,并且提供了OAuth - SAML - CAS ...
- 关于apache的几个常见问题
一.安装apache的时候总是不成功,提示:failed to open the winnt service manager 原因:软件安装时与windows7的"用户账户控制"( ...
- 20181026_队测_Brick Game
题目描述 给出一个\(n\)行\(m\)列的矩阵,矩阵中每个格子有一个非负整数,现在要求你去除其中的个格子,使得剩下的格子中的数的总和最大.另外,去除\(k\)个格子后,剩下的格子必须满足以下几个性质 ...
- 解决jar包冲突
参考文档: http://www.jianshu.com/p/100439269148 idea plugin: https://www.cnblogs.com/huaxingtianxia/p/57 ...
- 云服务器之——Linux下配置JDK环境
在Linux下jdk的安装已经操作了很多次,每次发现自己还是会忘记之前的操作,所以今天就简单的来做个记录. 第一步:下载jdk安装包 登录oracle官网:https://www.oracle.com ...
- 你相信吗:一加仑汽油可以给iPhone充电20年
一直以来,苹果公司的iPhone系列手机受到了全世界人民的喜欢,很多人就此成为了果粉.或许是由于我们过于在意iPhone系列手机出彩的外形,所以忽略了很多关于iPhone手机有意思的消息,我们今天就来 ...
- 吴裕雄--天生自然 python数据分析:健康指标聚集分析(健康分析)
# This Python 3 environment comes with many helpful analytics libraries installed # It is defined by ...
- angular知识点(2)
angular知识点(2) 1.为了代码规范,对于需要自动加载的依赖,需要在前面加上注释,注释为://@ngInject 或者是/*@ngInject*/ 2.ngSwitch的应用 在需要用到选择出 ...
- sql问题处理
批量杀死MySQL连接 select concat('KILL ',id,';') from information_schema.processlist where Info like 'selec ...