【问题标题】:Attribute error when testing Flask with PyTest fixture使用 PyTest 夹具测试 Flask 时出现属性错误
【发布时间】:2019-10-31 04:27:23
【问题描述】:

谁能解释我如何在我的测试中使用 pytest 固定装置?

我收到了这个conftest.py,它定义了 3 个 pytest 固定装置:

{...}

@pytest.fixture(scope='session', autouse=True)
def app(request):
    app = create_app({
        'TESTING': True
    })
    ctx = app.app_context()
    ctx.push()

    def teardown():
        ctx.pop()

    request.addfinalizer(teardown)
    return app

@pytest.fixture(scope='function')
def client(app, request):
    return app.test_client()

@pytest.fixture(scope='function')
def get(client):
    return humanize_werkzeug_client(client.get)

我正在尝试使用上述测试装置测试我的应用程序。根据我的理解,我需要在我的 pytests 中使用 app 夹具。正如在blog 中看到的那样,我尝试过这样的事情:

def test_myflaskapp(app):
    response = app.get('/')
    assert response.status_code == 200

但我收到一个属性错误:AttributeError: 'Flask' object has no attribute 'get'。这个answer 对我来说没有任何意义,恐怕我不确定它是否适用于我的情况。

有人可以解释我做错了什么吗?我正在努力学习Flask/PyTest,但我找不到解释其工作原理的示例/指南。

【问题讨论】:

    标签: python-3.x flask pytest


    【解决方案1】:

    app 夹具返回一个Flask 实例,我想你想在test client instance 上使用.get 方法。尝试在您的测试中将app 更改为client

    查看the official docs on testing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 2019-02-08
      • 2022-01-19
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 2022-07-14
      相关资源
      最近更新 更多