行为驱动开发 - 使用带有Flask的Python表现未定义的步骤

时间:2016-08-25 18:17:39

标签: python flask bdd python-behave

我正在关注Flask教程,目前正在使用Behave查看行为驱动开发。

我的任务是构建一个非常基本的博客应用程序,允许单个用户使用BDD登录,注销和创建博客帖子。

我编写了功能文件,步骤文件和环境文件。然后我编写了代码以使用户能够登录和退出。

当我在本地运行应用程序并手动测试时,它按预期工作,允许用户登录和注销并显示所需的文本(“您已登录”或“您已注销”),所以我是假设问题出在特征文件或步骤文件而不是应用程序代码中。

当我运行Behave时,最后一步似乎是“未定义”。

要素文件的相关部分是:

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

来自环境文件:

def before_feature(context, feature):
    context.client = app.test_client()

这是最后的“然后”步骤似乎是问题所在。我已经尝试检查教程解决方案并在别处搜索,但我似乎无法解决这部分代码。我必须对消息进行编码,因为我使用的是Python 3.5版本(如果这是相关的,教程使用的是2.7版本。)

非常感谢任何指针。

1 个答案:

答案 0 :(得分:0)

移动编码解决了问题。我现在有

@then(u'we should see the alert "{message}"')
def message(context, message):
    assert message.encode("utf-8") in context.page.data