如果我手动运行,这个简单的 Web 服务可以工作,但在我的单元测试中,我得到一个 404 not found 页面作为我的响应,阻止我正确测试应用程序。
正常行为:
文件夹结构:
/SRC
--web_application.py
/UNIT_TESTS
--test_wab_application.py
Run Code Online (Sandbox Code Playgroud)
web_application.py
from flask import Flask, request, jsonify, send_from_directory
from python.Greeting import Greeting
application = Flask(__name__)
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='mega_developer',
DATABASE=os.path.join(app.instance_path, 'web_application.sqlite'),
)
try:
os.makedirs(app.instance_path)
except OSError:
pass
return app
@application.route('/greetings', methods=['GET', 'POST'])
def hello():
# GET: for url manipulation #
if request.method == 'GET':
return jsonify(hello = request.args.get('name', 'world', str))
Run Code Online (Sandbox Code Playgroud)
test_web_application.py
import tempfile
import pytest
import web_application …Run Code Online (Sandbox Code Playgroud) testing automated-tests unit-testing flask http-status-code-404