【发布时间】:2020-04-20 19:11:44
【问题描述】:
使用 Pytest 固定装置,我正在寻找一种将设置覆盖传递给我的应用程序固定装置的方法,这样我就可以测试不同的设置而不必定义不同的固定装置。
在为 Flask 创建测试时,我使用一种通用模式来初始化应用程序和数据库,如下所示。请注意,db 夹具将 app 夹具硬编码为参数。
from myapp import create_app
@pytest.fixture
def app():
settings_override = {} # By setting values here, I can pass in different Flask config variables
app = create_app(settings_override)
return app
@pytest.fixture
def db(app):
do_something_to_create_the_database(app) # app needed for context
yield db
然后,许多测试可能会使用上面定义的固定装置,例如。
def test_my_application_1(db, app):
...
def test_my_application_2(db, app):
...
假设我想用不同的设置初始化应用程序夹具,假设我可以将这些设置传递给上面定义的 create_app() 函数。在每个测试的基础上,如何附加 app 和 db 夹具,以便我可以将设置覆盖传递给 app 夹具?有没有办法可以在 test case 级别参数化夹具,以便我可以将不同的设置传递给夹具?
即
# for this test, I want to pass the BAZ=True setting to the app fixture.
def test_my_application_1(db, app):
...
# for this test, I want to pass FOO=BAR setting to the app fixture
def test_my_application_2(db, app):
...
感谢您提供的任何建议。
更新:来自@mrbean-bremen 的解决方案
感谢@MrBean Bremen 提供优雅的解决方案。通过使用 hasattr 稍作修改,我能够扩展解决方案以接受参数覆盖或接受默认值。
@pytest.fixture(scope='function')
def app(request):
settings_override = {
'SQLALCHEMY_DATABASE_URI': "sqlite:///:memory:",
}
params = request.param if hasattr(request, 'param') else {}
return create_app({**settings_override, **params})
@pytest.fixture(scope='function')
def db(app):
with app.app_context():
....
def test_without_params(db, app):
...
@pytest.mark.parametrize("app", [{'DEBUG': True}], indirect=True)
def test_with_overrides(db, app):
...
【问题讨论】:
标签: python unit-testing flask pytest fixtures