简体   繁体   English

连接到Flask中的开发服务器进行单元测试

[英]Connect to dev server in Flask for unittest

I am trying to write unittests for my Flask API endpoints. 我正在尝试为我的Flask API端点编写单元测试。 I want the test cases to connect to the dev server which is running on a different port 5555. 我希望测试用例连接到在不同端口5555上运行的dev服务器。

Here is what I am doing in setUp() to make a test_client . 这是我在setUp()中做的一个test_client

import flask_app 
flask_app.app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqldb://root:@localhost/mvp_test_db'
flask_app.app.config['TESTING'] = True
flask_app.app.config['SERVER_NAME'] = '192.168.2.2' //the IP of the dev server
flask_app.app.config['SERVER_PORT'] = 5555
self.app_client = flask_app.app.test_client()

Then when I make a request using app_client like - 然后当我使用app_client发出请求时-

r = self.app_client.post('/API/v1/dummy_api', data = {'user_id' : 1})

I get a 404 when I print r and the request never comes to the dev server (no logs printed). 当我打印r时,我得到一个404,而该请求从未到达开发服务器(没有打印日志)。 I am not able to inspect the URL to which the connection is being attempted above. 我无法检查上面尝试连接到的URL。 Any ideas? 有任何想法吗?

It (app.test_client) does not send requests through network interfaces. 它(app.test_client)不会通过网络接口发送请求。 All requests are simulated. 所有请求都是模拟的。 These are processed inside werkzeug routing system. 这些在werkzeug路由系统内部进行处理。 To process "/API/v1/dummy_api" url you need register a view for. 要处理“ / API / v1 / dummy_api” URL,您需要注册一个视图。 If it is registered, connect it in the import section. 如果已注册,请在导入部分中将其连接。 Application settings and test settings almost always have almost equal settings. 应用程序设置和测试设置几乎总是具有几乎相等的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM