整体框架使用的是:Python+Unittest+Requests+PyMysql+HTMLReport 多线程并发模式

主要依赖模块 Unittest、Requests、PyMysql、HTMLReport,多线程的并发的一个好处在于能更快速的执行完数量庞大的接口自动化用例数

包含以下几个模块:

1. Business:与业务相关的公共模块

get_login_token:接口自动化过程中需要实时获取token,并将实时获取的token传给下个接口作为请求参数

from Business.url import url_login

import requests, json

def login_token(username=11111, password=123456):

"""获取登录后的token"""

headers = {'Content-Type': 'application/json;charset=UTF-8'}

request_param = {

"username": username,

"password": password

}

response = requests.post(url_login, data=json.dumps(request_param), headers=headers)

# 返回JSON中data数据的token

print(response.json()['data']['token'])

return response.json()['data']['token']

if __name__ == '__main__':

login_token()

headers:头部信息

headers = {

'Content-Type': "application/x-www-form-urlencoded",

'X-Requested-With': "XMLHttpRequest",

'Content-Length': "124",

'Connection': "keep-alive"

}

Common:与业务无关公共模块

connect_db:连接数据库,并操作数据库

import pymysql

# python3用的是pymysql,python2用的是MySQLdb

class OperationMysql:

"""

数据库SQL相关操作

"""

def __init__(self):

self.conn = pymysql.connect(

host='127.0.0.1',

port=3306,

user='test',

passwd='111111',

db='test',

charset='utf8',

cursorclass=pymysql.cursors.DictCursor

)

self.cur = self.conn.cursor()

# 查询一条数据

def search_one(self, sql):

self.cur.execute(sql)

result = self.cur.fetchone()  # 只显示一行结果

# result = self.cur.fetchall()  # 显示所有结果

return result

# 更新SQL

def updata_one(self, sql):

self.cur.execute(sql)

self.conn.commit()

self.conn.close()

if __name__ == '__main__':

op_mysql = OperationMysql()

res = op_mysql.search_one("SELECT *  from odi_order WHERE order_no='12222'")

print(res)

TestCase:测试用例层

test_case:外汇返佣

import unittest

from HTMLReport import logger

import requests

from Business.url import erp_url

class Category(unittest.TestCase):

"""ERP属性接口"""

def setUp(self):

self.session = requests.Session()

logger().info("获取会话")

def tearDown(self):

self.session.close()

logger().info("关闭会话")

def test_type_list(self):

"""get请求方式"""

s = self.session

querystry = {}

r = s.get(erp_url + '/xxx.list', params=querystry)

logger().info(f"返回数据{r.json()}")

self.assertEqual("success", r.json().get("msg"))

def test_pay_success_recommend(self):

"""Post请求方式"""

s = self.session

payload = {

"token": login_token,

"p": "ios",

"v": "5.6.0",

"order_no": "111111"

}

r = s.post(erp_url + '/xxxxx/aaa', data=payload)

logger().info(f"返回数据:{r.json()}")

self.assertEqual('success', r.json().get('msg'))

TestSuite:测试套件封装

suite_api:测试套件

import unittest

from Test_Case.refactor import test_order

def get_suite():

suite = unittest.TestSuite()

loader = unittest.TestLoader()

suite.addTests(loader.loadTestsFromTestCase(test_order.Apitests))

return suite

Run:主运行文件

import unittest

from Test_Suite import suite_api

import HTMLReport

import time

suite = unittest.TestSuite()

suite.addTests(suite_api.get_suite())

HTMLReport.TestRunner(

title="XXX项目测试报告",

description="测试人员:CesareCheung",

report_file_name=f"testreport",

thread_count=50

).run(suite)

原文链接:https://blog.csdn.net/weixin_42760923/article/details/103505791

Python+Unittest+Requests+PyMysql+HTMLReport 多线程并发接口化框架的更多相关文章

  1. Python+Unittest+Requests+PyMysql+HTMLReport 接口自动化框架

    整体框架使用的是:Python+Unittest+Requests+PyMysql+HTMLReport  多线程并发模式 主要依赖模块 Unittest.Requests.PyMysql.HTMLR ...

  2. 接口自动化-python unittest+requests+HTMLrunner

    从2015年毕业入行软件测试,快满4年了,之前技术分享都在百度贴吧上面,现在正式开始在博客中记录工作技术,努力成长,加油 接口测试的步骤1.组装好该接口需要的参数数据2.使用get或post附带参数数 ...

  3. python+unittest+requests+HTMLRunner编写接口自动化测试集

    问题描述:搭建接口测试框架,执行用例请求多个不同请求方式的接口 实现步骤: ① 创建配置文件config.ini,写入部分公用参数,如接口的基本url.测试报告文件路径.测试数据文件路径等配置项 [D ...

  4. python+unittest+requests实现接口自动化

    前言: Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2  ...

  5. 接口自动化框架(Pytest+request+Allure)

    前言: 接口自动化是指模拟程序接口层面的自动化,由于接口不易变更,维护成本更小,所以深受各大公司的喜爱. 接口自动化包含2个部分,功能性的接口自动化测试和并发接口自动化测试. 本次文章着重介绍第一种, ...

  6. python版接口自动化测试框架源码完整版(requests + unittest)

    python版接口自动化测试框架:https://gitee.com/UncleYong/my_rf [框架目录结构介绍] bin: 可执行文件,程序入口 conf: 配置文件 core: 核心文件 ...

  7. 转载:python + requests实现的接口自动化框架详细教程

    转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由 ...

  8. python+requests接口自动化测试框架实例详解教程

    1.首先,我们先来理一下思路. 正常的接口测试流程是什么? 脑海里的反应是不是这样的: 确定测试接口的工具 —> 配置需要的接口参数 —> 进行测试 —> 检查测试结果(有的需要数据 ...

  9. python+requests接口自动化测试框架实例详解

    python+requests接口自动化测试框架实例详解   转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...

随机推荐

  1. js百度地图API创建弧线并修改弧线的弧度

    去百度API官网下载CurveLine.min.js,注意复制下来的Js前面的行号要删除. // 百度地图API功能 var map = new BMap.Map("container&qu ...

  2. rem字体+百分比布局表格

    效果图: 上源码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. JMeter简单使用

    JMeter是apache公司基于java开发的一款开源压力测试工具.因为它是java开发的,所以运行的时候必须要安装jdk才可以:Jmeter是免安装的,所以拿到安装包后直接解压就可以使用了,它也是 ...

  4. JS中的getter和setter

    对象有两种属性:(1)数据属性,就是我们经常使用的属性(2)访问器属性,也称存取器属性 存取器属性就是一组获取和设置值的函数.getter负责获取值,它不带任何参数.setter负责设置值,在它的函数 ...

  5. [CF1161F]Zigzag Game

    通过这道模板题学了一种新的模型,记录一下. 稳定婚姻匹配 至于这道题,显然是一个二分图博弈的模型.考虑选择Bob,我们要找一组匹配使得任何情况下Bob都有匹配边能走.不失一般性假设Alice选择了in ...

  6. [HDU2294]Pendant

    题目:Pendant 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2294 分析: 1)f[i][j]表示长度为i,有j种珍珠的吊坠的数目. $f[i][ ...

  7. 富文本编辑器——百度UEditor插件安装教程

    一.使用环境 Win7 Eclipse jettty9 chrome 二.下载百度UEditor插件 1.下载地址:http://ueditor.baidu.com/website/download. ...

  8. oauth2学习

    oauth2 生词: 授权码模式(authorization code) 简化模式(implicit) 密码模式(resource owner password credentials) 客户端模式( ...

  9. English-spoken

    May i come in? 我可以进来么? May I introduce myself? 我能做个自我介绍么? I'm sorry I didn't hear that clearly. May ...

  10. display:inline-block在IE6/Ie7和IE8中的区别

    在IE6.IE7中不识别display:inline-block属性,但使用inline-block属性在IE下会触发layout,从而使内联元素拥有了display:inline-block属性的表 ...