【发布时间】: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