整体框架使用的是: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. Linux复制命令cp进阶

    cp -a:连同属性和权限也一起复制 cp -i:如果不带该参数,默认覆盖同名文件而不会提醒. cp -u:只拷贝符合以下条件的文件到目标目录:目标目录中不存在的文件或目标目录中文件版本较旧的文件. ...

  2. boost unordered

    Boost.Unordered provides the classes boost::unordered_set, boost::unordered_multiset, boost::unorder ...

  3. “TypeError: list indices must be integers or slices, not str”有关报错解决方案

  4. springBoot03- springboot+jpa+thymeleaf增删改查

    参考http://www.mooooc.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 数据库: CREATE TABLE ...

  5. ibatis 的使用

    1. 文本的使用 select  ‘day’+Num from Table;//Sql select convert(varchar,'day')+Num from Table;//ibatis

  6. Linux 下 VIM 的操作

    其实VI 和vim本质上没有多大区别,,但是VIM 可以高亮关键字,使得更受青睐 vim里面有3种模式:命令模式,编辑模式,末行模式 1. vim--->:set number VIM 打开文档 ...

  7. 使用Python的PIL模块来进行图片对比

    使用Python的PIL模块来进行图片对比 在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但是估计人会累死, ...

  8. 解决myeclipse validation验证javascript导致速度变慢的现象

    参考:https://jingyan.baidu.com/article/ca41422fe094251eae99ede7.html

  9. LeetCode 最短无序连续子数组

    题目链接:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/ 题目大意: 略. 分析: 如果排序区间为 [L ...

  10. Python中单下划线和双下划线

    1.双下划线开头和结尾 Python中存在一些特殊的方法,有些方法以双下划线 “__” 开头和结尾,它们是Python的魔法函数,比如__init__()和__str__等等.不用要这种方式命名自己的 ...