当前位置: 首页 > 工具软件 > pytest-flask > 使用案例 >

Flask-Testing 单元测试

辛渝
2023-12-01

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



 类似资料: