Flask-Testing 单元测试

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/bjbs_270/article/details/21050145

Flask-Testing官网:

https://pythonhosted.org/Flask-Testing/
http://flask.pocoo.org/docs/testing/

Flask 中文文档
http://dormousehole.readthedocs.org/en/latest/


准备

1. 安装pip
2. 安装Flask
3. 安装Flask-Testing (ver:>=0.4.1)

测试程序

#coding=utf8
from flask import Flask,jsonify
from flask.ext.testing import TestCase
import unittest

app = Flask(__name__)

@app.route("/ajax/")
def some_json():
    return jsonify(success=False)

class TestViews(TestCase):
    def create_app(self):
        app.config['TESTING'] = True
        return app

    def test_some_json(self):
        response = self.client.get("/ajax/")
        '''
               判断还回的JSON对像是不是{'success':True}
        '''
        self.assertEquals(response.json, dict(success=True))

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


执行测试结果:

python tests.py



展开阅读全文

没有更多推荐了,返回首页