Search code examples
pythoncucumberpython-behave

Python behave step definitions not implemented


I am currently attempting to learn cucumber tests in Python with behave. Every time I have an error message that states my tests are undefined. Can anyone please tell me what I am doing wrong?

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"

my 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))

When I run behave I get the following output

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

Solution

  • the error it´s in the "success" quotes:

    Feature:

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

    Step implementation:

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

    It should be: Feature:

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

    Or change the step implementation to:

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

    The error is in the quotes used in "success". The step implementation use the text: "success" (including the quotes) and assigne it to the txt variable and so, in the if statement, txt is NOT equal to success without the quotes.