Behave is a Python behavior-driven development framework that turns user-facing scenarios into executable tests. Scenarios use Given–When–Then steps to describe preconditions, actions, and expected outcomes. section 6.1.3
Asset manager behavior test
A feature can describe adding two assets: Given the Asset Manager is running, When two assets cost $2,000.00 each, Then their total cost is $4,000.00. Python step definitions initialize an in-memory repository, mock the location mediator, create assets, and assert the total. section 6.1.3
A practical Behave workflow is:
- Write a feature in user-story and Given–When–Then form. section 6.1.3
- Implement each phrase with Behave’s given, when, and then decorators. section 6.1.3
- Run the feature with behave features and inspect passed, failed, or skipped steps. section 6.1.3
|
|
We have a class called AssetManager. AssetManager's constructor takes two classes BaseRepository and AssetLocationMediator. The BaseRepository is a repository in which Assets are stored. We want to use InMemoryAssetRepository, which is a subclass of BaseRepository that stores the Assets in memory. Use a mock object for AssetLocationMediator. The AssetManager stores Assets using the method create and gets all of the Assets using the method get_assets. An asset has 10 required positional arguments: 'id', 'name', 'status', 'category', 'cost', 'useful_life', 'salvage_value', 'purchase_date', 'locations', and 'funding_details'. Use named parameters to make it clear which param is going where. Create a BDD script that will add two Assets to the AssetManager, confirming that all Assets cost $4000.00. This should be implemented in Python. Take it step by step, telling us the tools and libraries you will use. |
Explore a step-by-step Behave exercise for an AssetManager using an in-memory repository, a mock mediator, and named asset parameters. section 6.1.3
