bdd_wallet.py
Overview
`bdd_wallet.py` is a behavior-driven development (BDD) test module designed to verify the functionality of a simple wallet system using the `pytest-bdd` framework. The file defines a BDD scenario for purchasing fruits and ensures that the wallet's amount is correctly adjusted after buying apples and bananas. This module uses Gherkin language feature steps mapped to Python functions to simulate and validate wallet operations such as initializing the wallet, performing purchases, and checking the remaining balance.
Contents and Functionality
Scenario Definition
@scenario("bdd_wallet.feature", "Buy fruits")
def test_publish():
pass
Purpose: Links the BDD scenario described in the external
bdd_wallet.featurefile with the Python test runner.Parameters:
"bdd_wallet.feature": The file containing the Gherkin feature definitions."Buy fruits": The name of the specific scenario within the feature file.
Return Value: None. The decorator automatically generates a test case.
Usage: This function serves as a placeholder to trigger the scenario execution by pytest.
Fixture: wallet
@pytest.fixture
def wallet():
class Wallet:
amount = 0
return Wallet()
Purpose: Provides a reusable test fixture representing a wallet object with an initial amount set to 0.
Implementation Details: Defines a local class
Walletwith a single attribute:amount(int): Represents the current balance in the wallet.
Usage: Injected into test step functions to maintain state during scenario execution.
Step Definitions
These functions implement the Gherkin steps for the BDD scenario.
fill_wallet(wallet)
@given("A wallet with 50")
def fill_wallet(wallet):
wallet.amount = 50
Purpose: Sets up the initial state of the wallet with 50 units of currency.
Parameters:
wallet(Wallet): The wallet instance provided by the fixture.
Return Value: None.
Usage Example:
Given A wallet with 50Effect: Modifies the wallet's
amountto 50.
buy_apples(wallet)
@when("I buy some apples for 1")
def buy_apples(wallet):
wallet.amount -= 1
Purpose: Simulates purchasing apples costing 1 unit.
Parameters:
wallet(Wallet): The wallet instance.
Return Value: None.
Usage Example:
When I buy some apples for 1Effect: Decrements wallet amount by 1.
buy_bananas(wallet)
@when("I buy some bananas for 2")
def buy_bananas(wallet):
wallet.amount -= 2
Purpose: Simulates purchasing bananas costing 2 units.
Parameters:
wallet(Wallet): The wallet instance.
Return Value: None.
Usage Example:
When I buy some bananas for 2Effect: Decrements wallet amount by 2.
check(wallet)
@then("I have 47 left")
def check(wallet):
assert wallet.amount == 47
Purpose: Validates that the wallet has the expected amount after purchases.
Parameters:
wallet(Wallet): The wallet instance.
Return Value: None.
Usage Example:
Then I have 47 leftBehavior: Asserts that the wallet's amount equals 47. If the assertion fails, the test fails.
Important Implementation Details
The wallet is implemented as a simple class inside the fixture with a mutable
amountproperty, simulating a basic balance tracker.The BDD steps correspond directly to operations on the wallet:
givensets initial state.whenperforms actions.thenasserts outcomes.
The test scenario expects that buying apples and bananas sequentially reduces the wallet's balance from 50 to 47.
The test function
test_publishis empty but decorated with@scenarioto bind the feature file's scenario to pytest's test runner.
Interaction with Other Parts of the System
Feature File Dependency: This test file depends on an external feature file named
bdd_wallet.feature, which contains the Gherkin scenario definitions.pytest-bdd Framework: Uses pytest-bdd decorators and fixtures to connect Gherkin feature steps to Python code implementations.
pytest: Uses pytest fixtures (
@pytest.fixture) and assertion mechanisms to manage test lifecycle and validation.This file acts purely as a test module and does not interact with production code or business logic implementations beyond the simple
Walletstub class defined within.
Usage Example
To run the tests defined in this file, execute the following command in the terminal:
pytest bdd_wallet.py
This will:
Parse the linked
bdd_wallet.featurefile.Execute the scenario "Buy fruits".
Run the step implementations defined here.
Provide a pass/fail status based on the wallet balance assertion.
Mermaid Class Diagram
classDiagram
class Wallet {
+int amount
}
class bdd_wallet {
+test_publish()
+fill_wallet(wallet)
+buy_apples(wallet)
+buy_bananas(wallet)
+check(wallet)
+wallet() <<fixture>>
}
bdd_wallet --> Wallet : uses
Summary
`bdd_wallet.py` is a minimal but clear example of applying BDD testing with `pytest-bdd` for a wallet use case. It demonstrates how to define test scenarios, use fixtures, and implement step definitions that reflect business rules. The file is primarily a test harness and relies on an external `.feature` file to describe behavior in Gherkin syntax. This modular approach allows easy extension for more complex wallet operations or other domain-specific features.