Python 行为步骤定义未实现

Python behave step definitions not implemented

我目前正在尝试通过 behave 在 Python 中学习黄瓜测试。每次我收到一条错误消息,指出我的测试未定义。谁能告诉我我做错了什么?

我的test.feature

Feature: Python integration


  Scenario: Cucumber Tests
    Given I have a new "DVD" in my cart
      And I have a new "BOOK" in my cart
    When I click on "hello"
    Then I should see "success"

我的test.py

from behave import *


@given('I have a new {item} in my cart')
def step_impl(context, item):
    print("The item is: {}".format(item))


@when('I click on {link}')
def step_impl(context, link):
    print("I am clicking the link: {}".format(link))


@then('I should see {txt}')
def step_impl(context, txt):
    if txt not in ['success', 'error']:
        raise Exception("Unexpected text passed in.")

    print("Checking if I see the '{}' text".format(txt))
    print("PASS. I see the '{}' text".format(txt))

当我 运行 表现时,我得到以下输出

Feature: Python integration # test.feature:2

  Scenario: Cucumber Tests              # test.feature:5
    Given I have a new "DVD" in my cart # None
    And I have a new "BOOK" in my cart  # None
    When I click on "hello"             # None
    Then I should see "success"         # None


Failing scenarios:
  test.feature:5  Cucumber Tests

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

"success" 引号中的错误:

特征:

...
- Then I should see "success"
...

实施步骤:

...
@then('I should see {txt}')
def step_impl(context, txt):
    if txt not in ['success', 'error']:
...

应该是: 特征:

...
- Then I should see success
...

或将步骤实现更改为:

...
@then('I should see {txt}')
def step_impl(context, txt):
    if txt not in ['"success"', 'error']:
...

错误在 "success" 中使用的引号中。步骤实现使用文本:"success"(包括引号)并将其分配给 txt 变量,因此,在 if 语句中,txt 不等于没有引号的成功。