使用Flask-testing客户端,实现mock server自启动
1.创建mock_server
#mock_server.py from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/user',methods = ['GET']) def mock_user(): return jsonify({'id':1,'name':'Jane','age':18}) if __name__ == '__main__': app.run(port=5000)
2.创建启动mockserver基类:
#base.py import unittest import mock_server class MockServerUnittest(unittest.TestCase): """use for set up an http server which can use in test""" @classmethod def setUpClass(cls) -> None: cls.app = mock_server.app.test_client()
#此处使用client,无需关闭
3.测试程序应用mockserver基类:
from mock_server import app from flask import Flask from flask_testing import TestCase import unittest class TestUser(TestCase): def create_app(self): return app def test_user(self): response = self.client.get('/api/user') print(response.get_json()) if __name__ == '__main__': unittest.main()
标签:__,Flask,app,server,mock,自启动,import,Mock From: https://www.cnblogs.com/woniuguoguo/p/18295743