Mock server自启动
使用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 base import MockServerUnittest import unittest class TestUser(MockServerUnittest): def test_user1(self): response = self.app.get('/api/user') print(response.get_json()) def test_user2(self): response = self.app.get('/api/user') print("This is test2" + str(response.get_json())) if __name__ == '__main__': unittest.main()