liveBook · Manning

Behave

As explained in AI-Powered Developer: Build software with ChatGPT and Copilot

Behave is a Python behavior-driven development framework that executes natural-language scenarios and their step implementations.

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

How AI-Powered Developer: Build software with ChatGPT and Copilot covers it

How deep it goes
Defines GWT scenarios, contrasts unit and integration testing, and demonstrates implementation and execution without descending into framework internals.
Examples
Uses an AssetManager feature, two $2,000 assets, an in-memory repository, and a mocked AssetLocationMediator.
What it assumes
Assumes Python application components such as AssetManager and repositories while expressing behavior through scenarios.
What you can do after
Run executable scenarios with Behave and verify whether implemented behavior passes or fails.
How it connects
Connects behavior-driven development with user stories, unit testing, integration testing, and mock objects.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Developer: Build software with ChatGPT and Copilot. The section links point to the parts of the book it draws on.