Python表现未实现的步骤定义

时间:2018-05-26 05:47:41

标签: python cucumber python-behave

我目前正在尝试用Python来学习Python中的黄瓜测试。每次我有一条错误消息,表明我的测试未定义。谁能告诉我我做错了什么?

my 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

1 个答案:

答案 0 :(得分:1)

“成功”引号中的错误:

功能:

...
- 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']:
...

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