如何将集成测试应用于Flask RESTful API [英] How to apply integration tests to a Flask RESTful API

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

问题描述

[根据 https://stackoverflow.com/a/46369945/1021819 ,标题应为是指集成测试而不是单元测试]

[As per https://stackoverflow.com/a/46369945/1021819, the title should refer to integration tests rather than unit tests]

假设我想测试以下Flask API(来自此处):

Suppose I'd like to test the following Flask API (from here):

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)

将其另存为 flaskapi.py 并运行,在同一目录中,我运行脚本 test_flaskapi.py :

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()

两个测试都通过了,但是对于第二个测试(在 TestFlaskApi 中定义)类,我还没有弄清楚如何断言JSON响应符合预期(即,{'hello':'world'} ).这是因为它是 flask.wrappers.Response 的实例(本质上可能是Werkzeug响应对象(参见响应对象.

Both the 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.

如何对第二个 response 的JSON内容进行断言?

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

推荐答案

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

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
import sys

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().decode(sys.getdefaultencoding())), 
            {'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天全站免登陆