温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python中unittest如何实现api自动化测试

发布时间:2021-08-07 09:23:17 来源:亿速云 阅读:115 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“python中unittest如何实现api自动化测试”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“python中unittest如何实现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

以上是“python中unittest如何实现api自动化测试”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI