デコレータwith_app_context
で装飾されたunittesting(pytest)フラスコcliコマンド(クリック時)のテストアプリケーションのバージョンを正しく使用する方法がわかりません。このデコレータはpytest fixtureアプリを "通常の"開発アプリケーションに置き換えます。私はアプリの工場のパターンを使用します。フラスコクリックコマンドunittests - "with_appcontext"デコレータでテストアプリケーションを使う方法?
簡易版での私のコマンドは、この(心@with_appcontext
)のようになります。それとして、@with_appcontext
ユニットテストなし
@click.command()
@click.option('--username', prompt='Username')
@click.option('--email', prompt='User E-Mail')
@click.option('--password', prompt='Password', confirmation_prompt=True, hide_input=True)
@with_appcontext # from flask.cli import with_appcontext
def createsuperuser(username, email, password):
user = User(
username=username,
email=email,
password=password,
active=True,
is_admin=True,
)
user.save()
は(彼らはpytestによって注入アプリを取得)うまく動作するが、コマンド自体にはありませんアプリコンテキストが必要です。
マイ抽出pytestコード:app
とdb
器具を使用して
# pytest fixtures
@pytest.yield_fixture(scope='function')
def app():
"""An application for the tests."""
_app = create_app(TestConfig)
ctx = _app.test_request_context()
ctx.push()
yield _app
ctx.pop()
@pytest.yield_fixture(scope='function')
def db(app):
"""A database for the tests."""
_db.app = app
with app.app_context():
_db.create_all()
yield _db
# Explicitly close DB connection
_db.session.close()
_db.drop_all()
@pytest.mark.usefixtures('db')
class TestCreateSuperUser:
# db fixture uses app fixture, works the same if app was injected here as well
def test_user_is_created(self, cli_runner, db):
result = cli_runner.invoke(
createsuperuser,
input='johnytheuser\[email protected]\nsecretpass\nsecretpass'
)
assert result.exit_code == 0
assert 'SUCCESS' in result.output
# etc.
すべての私のテストは、これらの装飾が施されたものとは別にうまく動作します。私はこのアプリを設定するこのwith_appcontext
デコレータを回避する方法がわからない。
ありがとうございました。
悪いわけではないが、動作しているようです! –