如何单元测试Flask RESTful API [英] How to unit test a Flask RESTful API

查看:265
本文介绍了如何单元测试Flask RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想测试下面的Flask API(from
  import flask 
import flask_restful
$ b $ app = flask.Flask(__ name__)
api = flask_restful.Api应用)
$ b $ class HelloWorld(flask_restful.Resource):
def get(self):
return {'hello':'world'}

api.add_resource(HelloWorld,'/')

if __name__ ==__main__:
app.run(debug = True)

保存为 flaskapi.py 并运行它,在同一目录中运行脚本 test_flaskapi.py

  import unittest 
导入flaskapi
导入请求

class TestFlaskApiUsingRequests(unittest.TestCase):
def test_hello_world(self):
response = r equests.get('http:// localhost:5000')
self.assertEqual(response.json(),{'hello':'world'})


class TestFlaskApi(unittest.TestCase):
def setUp(self):
self.app = flaskapi.app.test_client()
$ b def test_hello_world(self):
response = self.app.get('/')

if __name__ ==__main__:
unittest.main()

这两个测试都通过了,但是对于第二个测试(定义在 TestFlaskApi )类,我没有还想出了如何断言JSON响应如预期的那样(即, {'hello':'world'} )。这是因为它是的一个实例。flask.wrappers.Response (这实际上可能是一个Werkzeug响应对象(参见 http://werkzeug.pocoo.org/docs/0.11/wrappers/ )),而且我还找不到相同的 json()方法用于请求' Response object。



如何在JSON内容第二个响应

解决方案

我发现我可以通过将 json.loads()应用于 get_data()方法的输出来获取JSON数据:

  import unittest 
导入flaskapi
导入请求
导入json

类TestFlaskApiUsingRequests(unittest.TestCase):
def test_hello_world(self):
response = r equests.get('http:// localhost:5000')
self.assertEqual(response.json(),{'hello':'world'})


class TestFlaskApi(unittest.TestCase):
def setUp(self):
self.app = flaskapi.app.test_client()
$ b def test_hello_world(self):
response = self.app.get('/')
self.assertEqual(json.loads(response.get_data()),{'hello':'world'})


if __name__ ==__main__:
unittest.main()

这两个测试按需要传递:

  .. 
-------------- -------------------------------------------------- ------
在0.019s中执行2次测试


[在0.3s中完成]


Suppose I'd like to test the following Flask API (from http://flask-restful-cn.readthedocs.io/en/0.3.5/quickstart.html#a-minimal-api):

import flask
import flask_restful

app = flask.Flask(__name__)
api = flask_restful.Api(app)

class HelloWorld(flask_restful.Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/')

if __name__ == "__main__":
    app.run(debug=True)

Having saved this as flaskapi.py and run it, in the same directory I run the script test_flaskapi.py:

import unittest
import flaskapi
import requests

class TestFlaskApiUsingRequests(unittest.TestCase):
    def test_hello_world(self):
        response = requests.get('http://localhost:5000')
        self.assertEqual(response.json(), {'hello': 'world'})


class TestFlaskApi(unittest.TestCase):
    def setUp(self):
        self.app = flaskapi.app.test_client()

    def test_hello_world(self):
        response = self.app.get('/')

if __name__ == "__main__":
    unittest.main()

Both tests pass, but for the second test (defined in the TestFlaskApi) class I haven't yet figured out how to assert that the JSON response is as expected (namely, {'hello': 'world'}). This is because it is an instance of flask.wrappers.Response (which is probably essentially a Werkzeug Response object (cf. http://werkzeug.pocoo.org/docs/0.11/wrappers/)), and I haven't been able to find an equivalent of the json() method for requests' Response object.

How can I make assertions on the JSON content of the second response?

解决方案

I've found that I can get the JSON data by applying json.loads() to the output of the get_data() method:

import unittest
import flaskapi
import requests
import json

class TestFlaskApiUsingRequests(unittest.TestCase):
    def test_hello_world(self):
        response = requests.get('http://localhost:5000')
        self.assertEqual(response.json(), {'hello': 'world'})


class TestFlaskApi(unittest.TestCase):
    def setUp(self):
        self.app = flaskapi.app.test_client()

    def test_hello_world(self):
        response = self.app.get('/')
        self.assertEqual(json.loads(response.get_data()), {'hello': 'world'})


if __name__ == "__main__":
    unittest.main()

Both tests pass as desired:

..
----------------------------------------------------------------------
Ran 2 tests in 0.019s

OK
[Finished in 0.3s]

这篇关于如何单元测试Flask RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆