烧瓶API路线的单元测试

时间:2018-05-30 23:02:30

标签: python python-3.x unit-testing flask

我正在建立一个简单的烧瓶API并且我对它很新,我已经在/my_app_name/main/controllers.py中有大约8条路线,我想为它们编写单元测试。其中一些路由使用在请求正文中收到的json做某事:

@main.route('/update_account', methods=['POST'])
def update_account():

    """Updates an account in the DB."""

    ##########################################
    # gets the item

    item = request.json

    # code to add to the DB

    return "Success!"

如何为此编写单元测试?还有什么是正确的目录来进行测试?

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以在Flask's documentation for testing中获得Q1的答案。具体而言,有一章名为Testing JSON APIs

截至Q2,典型的结构可能是这样的:

| my_app_pkg
| tests
| setup.py

答案 1 :(得分:0)

有很多方法可以做到这一点;

one could be creating a models file and include classes to contain all the data and 
logic you have included in the update route.So in your  unittests file you import 
those modals and test for the functions....In your routes you could just call for the 
results from the modals

此外,您也可以将main导入到tsts文件中,并从类似这样的内容开始

 res = self.client().post('/update_account', data={[the json your api takes]}
    self.assertEqual(res.status_code, 201)
相关问题