python 不检测步骤文件的行为

python behave not detecting steps file

我是 Python 和 Behave 的新手。我正在尝试为我的自动化项目设置 POC。我遵循了 behave 文档中的教程,但是当我 运行 behave 时,它​​抛出的步骤是不可检测的。我错过了什么?

我的文件夹结构是这样的。

/features/testone.feature
/features/steps/testone_steps.py

特征文件

Feature: Running sample feature

@smoke @regression
Scenario: Login to github and verify it works in testone feature of scenario one
Given Launch GITHUB app with "test@test.com" and "githubtest"

步骤文件

from behave import given, when, then, step

@given(u'Launch GITHUB app with "{text}" and "{text}"')
def step_impl(context, user, password):
    print(f"This is the given step with {user} and {password}")

输出

λ behave
Feature: Running sample feature # features/testone.feature:1

  @smoke @regression
  Scenario: Login to github and verify it works in testone feature of scenario one  # features/testone.feature:4
    Given Launch GITHUB app with "test@test.com" and "githubtest"                   # None


Failing scenarios:
  features/testone.feature:4  Login to github and verify it works in testone feature of scenario one

0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
0 steps passed, 0 failed, 0 skipped, 1 undefined
Took 0m0.000s

You can implement step definitions for undefined steps with these snippets:

@given(u'Launch GITHUB app with "test@test.com" and "githubtest"')
def step_impl(context):
    raise NotImplementedError(u'STEP: Given Launch GITHUB app with "test@test.com" and "githubtest"')

我在我的 vscode 编辑器中注意到,在步骤文件中,pylint 显示了这条消息。

[pylint] E0611:No name 'given' in module 'behave'
[pylint] E0611:No name 'when' in module 'behave'
[pylint] E0611:No name 'then' in module 'behave'
[pylint] E0611:No name 'step' in module 'behave'

您的问题是您在步骤文件中使用的格式不正确。试试这个:

from behave import given, when, then, step

@given(u'Launch GITHUB app with "{user}" and "{password}"')
def step_impl(context, user, password):
    print("This is the given step with {user} and {password}".format(user, password))

请注意,@given 语句中定义的参数与 step_impl() 中传递的参数匹配。

也就是说,如果在 @given 中,您有

@given(u'Launch GITHUB app with "{用户}" and "{密码}"')

然后在您的步骤实施中,您应该

def step_impl(context,用户,密码)

如果没有此匹配项,在您当前的代码中,您将收到 NotImplementedError,因为 behave 正在寻找使用 contexttext 作为步骤中的参数的步骤实现文件,即 def step_impl(context, text).