python unittest实现api自动化测试
作者:老鼠程序员 发布时间:2022-12-05 09:52:37
项目测试对于一个项目的重要性,大家应该都知道吧,写python的朋友,应该都写过自动化测试脚本。
最近正好负责公司项目中的api测试,下面写了一个简单的例子,对API 测试进行梳理。
首先,编写restful api接口文件 testpost.py,包含了get,post,put方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import request
from flask_restful import Resource
from flask_restful import reqparse
test_praser = reqparse.RequestParser()
test_praser.add_argument('ddos')
class TestPost(Resource):
def post(self, PostData):
data = request.get_json()
user = User('wangjing')
if data['ddos']:
return {'hello': 'uese', "PostData": PostData, 'ddos': 'data[\'ddos\']'}
return {'hello': 'uese', "PostData": PostData}
def get(self, PostData):
data = request.args
if data and data['ddos']:
return "hello" + PostData + data['ddos'], 200
return {'hello': 'uese', "PostData": PostData}
def put(self, PostData):
data = test_praser.parse_args()
if data and data['ddos']:
return "hello" + PostData + data['ddos'], 200
return {'hello': 'uese', "PostData": PostData}
ps:对于request的取值,我这里定义了常用的三种方法:
post方法:request.get_json(),在调用API时,传值是json方式
get和put方法:request.args 或者reqparse.RequestParser(),调用API时,传的是字符串
其次,定义Blueprint(蓝图)文件 init.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Blueprint
from flask_restful import Api
from testpost import TestPost
testPostb = Blueprint('testPostb', __name__)
api = Api(testPostb)
api.add_resource(TestPost, '/<string:PostData>/postMeth')
然后,编写测试脚本testPostM.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import json
from secautoApp.api.testPostMeth import api
from flask import url_for
from run import app
from secautoApp.api.testPostMeth import TestPost
headers = {'Accept': 'application/json',
'Content-Type': 'application/json'
}
class APITestCase(unittest.TestCase):
def setUp(self):
# self.app = create_app(os.getenv("SECAUTOCFG") or 'default')
self.app = app
# self.app_context = self.app.app_context()
# self.app_context.push()
self.client = self.app.test_client()
#
# def tearDown(self):
# self.app_context.pop()
def test_post(self):
# with app.test_request_context():
response = self.client.get(api.url_for(TestPost, PostData='adb', ddos='123'))
self.assertTrue(response.status_code == 200)
response = self.client.get(url_for('testPostb.testpost', PostData='adb', ddos='123'))
self.assertTrue(response.status_code == 200)
self.assertTrue(json.loads(response.data)['PostData'] =='adb')
response = self.client.post(url_for('testPostb.testpost', PostData='adb'), headers=headers,
data=json.dumps({"ddos": '123'}))
print json.loads(response.data)
self.assertTrue(response.status_code == 200)
response = self.client.put(url_for('testPostb.testpost', PostData='adb', ddos='123'))
self.assertTrue(json.loads(response.data) == 'helloadb123')
response = self.client.put(url_for('testPostb.testpost', PostData='adb'))
print json.loads(response.data)['PostData']
self.assertTrue(response.status_code == 200)
ps:调用的api url 主要用的是flask_restful 的api.url_for,或者是flask的url_for,下面我来说下这2种方法的具体使用
flask_restful 的api.url_for说明
api.url_for(TestPost,PostData='adb'),这里的TestPost指的是restful api接口文件中定义的class,因为我们在api蓝图中,已经通过api.add_resource(TestPost, ‘//postMeth')添加类的方式定义过
flask的url_for的使用说明
url_for(‘testPostb.testpost', PostData='adb', ddos='123'),'testPostb.testpost'这个字符串中
testPostb指的是蓝图的名称,也就是testPostb = Blueprint(‘testPostb', name)中Blueprint(‘testPostb',name)中的testPostb。
testpost指的是蓝图下endpoit的端点名称,flask_restful中,指的是api.add_resource(TestPost, ‘//postMeth')中 类名TestPost的小写
启动测试脚本:
C:\secauto3>python run.py test
test_post (testPostM.APITestCase) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.056s
OK
小总结:url_for的传值和request中的取值是有对应关系的,最后就是flask_restful中端点的方式,一定要是api.add_resource中类名的小写。
领取干货:零基础入门学习python视频教程
来源:https://blog.csdn.net/u010445540/article/details/56676003
猜你喜欢
- 大家都知道JAVA里最流行的是MVC模型的编程方式,如果你不知道MVC的概念,可以去网上搜索下,应该会马上找到N多资料。PHP5推出之后,也
- jinjia和vue.js默认的模板转义符都是{{}}目前的解决办法是修改vue.js的转义符,将原来的{{}}替换为其他标签,我改为{[]
- javascript版 俄罗斯方块(Russian box)小游戏,喜欢的朋友可以玩玩。对源代码感兴趣的朋友也可以研究一下。玩法介绍:可以输
- 制作自己的训练集下图是我们数据的存放格式,在data目录下有验证集与测试集分别对应iris_test, iris_train 为了向伟大的M
- Python编程时,经常需要跳过第一行读取文件内容。简单的做法是为每行设置一个line_num,然后判断line_num是否为1,如果不等于
- 首先我们知道这个效果应该是一个老话题了。今天整理文件的时候,发现自己以前的一些布局的解决方法躺在文件夹里很长时间了,翻翻老底吧。需要说明的是
- 零、本讲学习目标了解面向对象编程思想掌握类和对象的定义和使用了解Python中的对象一、面向对象(一)程序员“面向对象”在现实世界中存在各种
- 前言本文主要给大家介绍了关于Python用字符串调用函数或方法的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍:先看一个例子:&
- 求最大公约数是习题中比较常见的类型,下面小编会给大家提供五种比较常见的算法,记得帮忙点个赞哦!一般来说,最大公约数的求法大概有5种方法一:短
- python中正则表达式中的匹配次数问题网上有很多解释,最多的就是*匹配0或者无数次,+匹配1次或无数次,?匹配0次或者1次。可是虽然这个文
- 1.图例legend基础语法及用法legend语法参数如下: matplotlib.pyplot.legend(*args, **
- 先写一个批处理文件,给个例子。 代码如下:set rq=%date:~0,10% exp system/system的
- 一、安装pip install apscheduler二、ApScheduler 简介1 APScheduler的组件triggers:触发
- 原来sql还有个stuff的函数,很强悍。 一个列的格式是单引号后面跟着4位的数字,比如'0003,'0120,'4
- 这篇论坛文章详细介绍了完全卸载MySQL数据库5.0的具体方法,更多内容请参考下文:数据库突然出了问题,没办法只能重装,因为事先并不知道My
- 一、字典、元组的多重嵌套例 1:记录全班学生的成绩。分析:定义一个 SimpleGradebook类,学生名是字典self._grades的
- 今天在测试以下代码时遇到该错误:session_start();$_SESSION['username']=$usernam
- django中瀑布流初探img.html<!DOCTYPE html><html lang="en"&
- 即使在urlencode之前str.decode(“cp936″).encode(“utf-8″)做了编码转换也是没用的。后来查询手册查到一
- 前言在对DataFrame数据进行处理时,存在需要对数据内容进行遍历的场景。因此记录一下按照行,列遍历的几种方式。一、按行遍历1. 使用lo