项目测试对于一个项目的重要性,大家应该都知道吧,写python的朋友,应该都写过自动化测试脚本。
最近正好负责公司项目中的api测试,下面写了一个简单的例子,对API 测试进行梳理。
首先,编写restful api接口文件 testpost.py,包含了get,post,put方法
#!/usr/bin/env python# -*- coding: utf-8 -*-from flask import requestfrom flask_restful import Resourcefrom flask_restful import reqparsetest_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)
新闻热点
疑难解答