本文摘自php中文网,作者不言,侵删。
这篇文章主要为大家详细介绍了python unittest实现api自动化测试的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下项目测试对于一个项目的重要性,大家应该都知道吧,写python的朋友,应该都写过自动化测试脚本。
最近正好负责公司项目中的api测试,下面写了一个简单的例子,对API 测试进行梳理。
首先,编写restful api接口文件 testpost.py,包含了get,post,put方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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
1 2 3 4 5 6 7 8 9 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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 = app
self .client = self .app.test_client()
def test_post( self ):
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的小写
启动测试脚本:
1 2 3 4 5 6 7 | 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如何使用unittest测试接口_python
python+requests+unittest API接口测试问题
以上就是python unittest实现api自动化测试_python的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python学会后做什么
Python中转义字符是什么意思
Python里join是什么意思
最详细的Python库总结
Python实现网页中下载的功能实例
学习如何正确使用Python临时文件
如何调用Python中的内置函数?(实例解析)
Python运行快捷键是什么
Python数据分析与应用是什么
Python实现按当前日期(年、月、日)创建多级目录的方法
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python unittest实现api自动化测试_python