我有这个Flask
应用:
myapp.py :
def create_app():
app = Flask(__name__)
return app
app = create_app()
session = session_factory()
def connect_db():
engine = get_engine()
return engine.connect()
@app.route("/")
def hello():
return "Hello everyone"
我想编写一些pytest测试,所以我创建了两个文件:
conftest.py:
import pytest
from app.myapp import create_app
@pytest.fixture
def app():
app = create_app()
return app
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def runner(app):
return app.test_cli_runner()
test_endpoints.py
import pytest
from flask import *
def test_hello(client):
response = client.get("/")
assert response.data == b'Hello everyone'
运行pytest时,出现此错误:
> assert response.data == b'Hello everyone'
E assert '<!DOCTYPE HT... again.</p>\n' == 'Hello everyone'
E + Hello everyone
E - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
E - <title>404 Not Found</title>
E - <h1>Not Found</h1>
E - <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
为什么pytest
找不到端点?我可以在这里添加些什么才能使其正常工作?
答案 0 :(得分:2)
您不会在从固定装置中的app
获得的create_app()
上添加任何路由。
如果您考虑自己的代码
def create_app():
app = Flask(__name__)
return app
app = create_app()
# ...
@app.route("/")
def hello():
return "Hello everyone"
,该hello
路由仅添加到该全局app
实例,而不是您从灯具中获取的路由。
Flask教程,line 573,covers testing。您可能也想这样做。像这样的东西。
bp = Blueprint('greetings', __name__)
@bp.route("/")
def hello():
return "Hello everyone"
def create_app():
app = Flask(__name__)
app.register_blueprint(bp)
return app