1 Star 0 Fork 0

sheerydai / apiTestP2P

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
common.py 4.88 KB
一键复制 编辑 原始数据 按行查看 历史
sheerydai 提交于 2022-05-11 21:02 . Initial commit
# 封装断言为标准方法
import json
import requests
from bs4 import BeautifulSoup
from utils import GetLogger
import pymysql
import config
logger = GetLogger().init_logger()
def assert_common(self, res, status_code, status, description):
self.assertEqual(status_code, res.status_code)
self.assertEqual(status, res.json().get('status'))
self.assertEqual(description, res.json().get('description'))
# 封装第三方接口请求
def third_requests_api(form_data):
# 解析form表单中的内容,并提取第三方请求的参数
soup = BeautifulSoup(form_data, 'html.parser')
third_trust_url = soup.form['action']
logger.info('third trust url = {}'.format(third_trust_url))
data = {}
for input in soup.find_all('input'):
data.setdefault(input['name'], input['value'])
logger.info('third request data = {}'.format(data))
# 发送第三方开户请求
res = requests.post(third_trust_url, data=data)
# 注意:封装第三方请求时一定要记得返回,后面还要做断言等处理
return res
# 读取图片验证码参数化文件方法
def read_img_verify_code(file_name):
file = config.BASE_DIR + '/data/' + file_name
test_data = []
with open(file, encoding='utf-8') as f:
buf = json.load(f)
for data in buf.get('get_img_code_case'):
test_data.append((data.get('type'), data.get('status_code')))
# print('test data = {}'.format(test_data))
return test_data
# 读取注册参数化文件方法
def read_register_data(file_name):
file = config.BASE_DIR + '/data/' + file_name
test_data = []
with open(file, encoding='utf-8') as f:
buf = json.load(f)
for data in buf.get('test_register_case'):
test_data.append((data.get('phone'), data.get('pwd'), data.get('img_code'), data.get('phone_code'),
data.get('dy_server'), data.get('invite_phone'), data.get('status_code'),
data.get('status'), data.get('description')))
print('test data = {}'.format(test_data))
return test_data
# 定义统一的读取所有参数数据文件的方法
def read_para_data(filename, case_name, params):
# filename: 参数数据文件的文件名
# case_name: 参数数据文件中定义的测试数据列表的名称,如:test_get_img_verify_code
# params: 参数数据文件一组测试数据中所有的参数组成的字符串,如:"type,status_code"
file = config.BASE_DIR + '/data/' + filename
test_data = []
with open(file, encoding='utf-8') as f:
# 将json字符串转换为字典格式
buf = json.load(f)
# 循环获取所有的测试数据的列表
for data in buf.get(case_name):
# 先将data对应的一组测试数据,全部读取出来,并生成一个列表
params_list = []
for param in params.split(','):
# 依次获取同一组测试数中每个参数的值,添加到params_list中,形成一个列表
params_list.append(data.get(param))
# 每完成一组测试数据的读取,就添加到test_data后,直到所有的测试数据读取完毕
test_data.append(params_list)
print('test data = {}'.format(test_data))
return test_data
class DBUtils:
# 初始化
__conn = None
__cursor = None
# 创建连接
@classmethod
def __get_conn(cls, db_name):
if cls.__conn is None:
cls.__conn = pymysql.connect(host=config.DB_URL,
port=3306,
user=config.DB_USERNAME,
password=config.DB_PASSWORD,
database=db_name,
charset="utf8",
autocommit=True
)
return cls.__conn
# 创建游标
@classmethod
def __get_cursor(cls):
if cls.__cursor is None:
cls.__cursor = cls.__get_conn().cursor()
return cls.__cursor
# 执行sql
@classmethod
def exe_sql(cls, db_name, sql):
try:
cursor = cls.__get_cursor(db_name)
cursor.execute(sql)
if sql.split()[0].lower() == "delete":
return cursor.fetchall()
else:
cls.__conn.commit()
return cursor.rowcount
except Exception as e:
print(e)
cls.__conn.rollback()
finally:
cls.__close_cursor()
cls.__close_conn()
# 关闭游标
@classmethod
def __close_cursor(cls):
if cls.__cursor:
cls.__cursor.close()
cls.__cursor = None
# 关闭连接
@classmethod
def __close_conn(cls):
if cls.__conn:
cls.__conn.close()
cls.__conn = None
1
https://gitee.com/sheerysong/apiTestP2P.git
git@gitee.com:sheerysong/apiTestP2P.git
sheerysong
apiTestP2P
apiTestP2P
master

搜索帮助