私はFlaskチュートリアルに従っており、現在Behaveを使用してBehavior Driven Developmentを見ています。動作駆動型開発 - FlaskでPythonを使用して動作する未定義のステップ
私の仕事は、BDDを使用して、1人のユーザーがログインしてログアウトしてブログ投稿を作成できる非常に基本的なブログアプリケーションを構築することです。
フィーチャーファイル、ステップファイル、および環境ファイルを作成しました。私はその後、ユーザーがログインとログアウトを可能にするコードを書いています。
アプリケーションをローカルで実行して手動でテストすると、予想どおりに動作し、ユーザーがログインとログアウトを許可し、必要なテキスト(「ログインしました」または「ログアウトしました」)問題は、アプリケーション・コードではなく、フィーチャー・ファイルまたはステップ・ファイルであると仮定しています。
私がBehaveを実行すると、最後のステップは「未定義」と表示されます。 「
def before_feature(context, feature):
context.client = app.test_client()
それは最終的なもの:
Feature: flaskr is secure in that users must log in and log out to access certain features
Scenario: successful login
Given flaskr is setup
When we log in with "admin" and "admin"
Then we should see the alert "You were logged in"
Scenario: successful logout
Given flaskr is setup
and we log in with "admin" and "admin"
When we log out
Then we should see the alert "You were logged out"
そして、私の手順ファイルは次のとおりです:
機能ファイルの関連部分は、環境ファイルから
from behave import *
@given(u'flaskr is setup')
def flask_is_setup(context):
assert context.client
@given(u'we log in with "{username}" and "{password}"')
@when(u'we log in with "{username}" and "{password}"')
def login(context, username, password):
context.page = context.client.post('/login',
data=dict(username=username,
password=password),
follow_redirects=True
)
assert context.page
@when(u'we log out')
def logout(context):
context.page = context.client.get('/logout', follow_redirects=True)
assert context.page
@then(u'we should see the alert "{message.encode("utf-8")}"')
def message(context, message):
assert message in context.page.data
それでは "問題があるように見えるステップ。私はチュートリアルのソリューションをチェックし、他の場所で検索しようとしましたが、コードのこの部分を解決できないようです。私はPythonバージョン3.5を使用しているときにメッセージをエンコードしなければなりませんでした(このチュートリアルでは、これが適切であればバージョン2.7を使用していました)。
どのポインタも非常に高く評価されます。
あなたはこの方法の中でエンコードを移動しようとしましたか?多分正規表現がこれのために一致しません。 – lauda
ありがとう、これは私の問題を解決しました。少なくとも私はこれがあなたが意味するものだと思います。私はcontext.page.data'に 'assert message.encode(" utf-8 ")を持つようにエンコードを移動しました。 – James
はい、これは私が意味していたものです。 – lauda