为什么以下 pytest-bdd 代码会抛出错误:
pytest_bdd.exceptions.StepDefinitionNotFoundError:找不到步骤定义:给出“我有一个空的购物车
这是功能文件:
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 |
以下是步骤:
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
奇怪的是,当我将功能文件中的“Given”更改为“When”并在步骤代码中执行相同操作时,代码有效。
代码来源:来自 Medium (Ramkumar R)