Step definition not found error – for the Given step only — pytest-bdd

Why is the following pytest-bdd code throwing an error:

pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found: Given “I have an empty shopping cart

Here is the feature file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Feature: Shopping Cart
As a customer
I want to add products to my shopping cart
So that I can purchase them later
Scenario Outline: Add products to the shopping cart
Given I have an empty shopping cart
When I add <product> to the cart
Then the cart should contain <product> and shelf number is <shelf_num>
Examples:
| product | shelf_num |
| Apples | 5 |
| Bananas | 24 |
| Oranges | 11 |
</code>
<code>Feature: Shopping Cart As a customer I want to add products to my shopping cart So that I can purchase them later Scenario Outline: Add products to the shopping cart Given I have an empty shopping cart When I add <product> to the cart Then the cart should contain <product> and shelf number is <shelf_num> Examples: | product | shelf_num | | Apples | 5 | | Bananas | 24 | | Oranges | 11 | </code>
Feature: Shopping Cart
              As a customer
              I want to add products to my shopping cart
  So that I can purchase them later

        Scenario Outline: Add products to the shopping cart
            Given I have an empty shopping cart
             When I add <product> to the cart
             Then the cart should contain <product> and shelf number is <shelf_num>

        Examples:
                  | product | shelf_num |
                  | Apples  | 5         |
                  | Bananas | 24        |
                  | Oranges | 11        |

Here is the steps:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>scenarios('scenario_example_demo.feature')
@pytest.fixture
def shopping_cart():
return []
@given('I have an empty shopping cart')
def empty_shopping_cart(shopping_cart):
assert len(shopping_cart) == 0
@when(parsers.parse('I add {product} to the cart'))
def add_product(shopping_cart, product):
shopping_cart.append(product)
@then(parsers.parse('the cart should contain {product} and shelf number is {shelf_num}'))
def verify_cart_contents(shopping_cart, product,shelf_num):
assert product in shopping_cart and int(shelf_num)>0
</code>
<code>scenarios('scenario_example_demo.feature') @pytest.fixture def shopping_cart(): return [] @given('I have an empty shopping cart') def empty_shopping_cart(shopping_cart): assert len(shopping_cart) == 0 @when(parsers.parse('I add {product} to the cart')) def add_product(shopping_cart, product): shopping_cart.append(product) @then(parsers.parse('the cart should contain {product} and shelf number is {shelf_num}')) def verify_cart_contents(shopping_cart, product,shelf_num): assert product in shopping_cart and int(shelf_num)>0 </code>
scenarios('scenario_example_demo.feature') 

@pytest.fixture
def shopping_cart():
    return []

@given('I have an empty shopping cart')
def empty_shopping_cart(shopping_cart):
    assert len(shopping_cart) == 0

@when(parsers.parse('I add {product} to the cart'))
def add_product(shopping_cart, product):
    shopping_cart.append(product)

@then(parsers.parse('the cart should contain {product} and shelf number is {shelf_num}'))
def verify_cart_contents(shopping_cart, product,shelf_num):
    assert product in shopping_cart and int(shelf_num)>0

Weirdly, when I change the ‘Given’ to ‘When’ in the feature file and do same in the steps code, the code works.

code credits: from Medium (Ramkumar R)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật