Pytest-为什么这个测试失败了?在应用程序上下文之外工作 & 找不到 404 请求 URL 中断言 b'404 未找到

问题描述 投票:0回答:0

我正在尝试对我的应用程序进行 pytest,首先检查主页是否正确返回。该应用程序在前端工作,所以相信它设置正确我只是无法让 pytest 工作。路由是“/home/”,因为“/”是登录页面。代码如下:

会议.py

@pytest.fixture(scope='session')
def app():
    app = Flask(__name__)
    app.config['TESTING'] = True
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    db = SQLAlchemy(app)

    with app.app_context():
        db.create_all()
        yield app

    db.drop_all()


@pytest.fixture(scope='session')
def client(app):
    with app.test_client() as client:
        yield client

app_test.py

def test_home(client):
    response = client.get("/home/")
    assert b'<title>Home</title>' in response.data

HTML文件:

{% extends "layout.html" %}
{% block title %}
Home
{% endblock %}
{% block content %}
<!-- Creating the button that will take you to the area that you can raise a new ticket -->
<button class='new_ticket'><a href="{{url_for('new_ticket')}}" class='btn btn-success'>New Ticket</a></button>
<br>
<p>Welcome to App.</p>
{% endblock %}

路线:

@app.route("/home/")
@login_required
def home():
    return render_template("home.html") 

从上面的代码收到的错误消息: 失败的测试/app_test.py::test_home - AssertionError:在 b' 中断言 b'Home' 404 未找到

找不到

在服务器上找不到请求的 URL。如果您手动输入 URL,请检查您的拼写并重试

错误测试/app_test.py::test_home - RuntimeError:在应用程序上下文之外工作。

不确定接下来要尝试什么或哪里出了问题。希望通过考试

python flask pytest flask-sqlalchemy
© www.soinside.com 2019 - 2024. All rights reserved.