liveBook · Manning

Pytest-bdd

As explained in Build Financial Software with Generative AI (From Scratch)

pytest-bdd is a Python plugin that integrates behavior-driven development scenarios with pytest tests.

pytest-bdd connects behavior-driven development scenarios with pytest tests. It is useful when an API test should express stakeholder-readable behavior rather than only isolated implementation details. section 4.3.1

Build a scenario-driven API test in a few steps:

  • Install pytest-bdd, then create tests/features for .feature files and tests/step_defs for Python step definitions. section 4.3.1
  • Describe behavior with Given/When/Then language, such as making a GET request to /files and expecting status code 200. section 4.3.1
  • Load scenarios with scenarios(), connect steps using decorators and parsers, and share the response through a pytest fixture. section 4.3.1

Testing the /files endpoint

A feature can state: “When I make a GET request to the endpoint /files, Then the status code should be 200.” The matching step definition calls TestClient.get(), stores the response, and asserts its status code. Unsupported methods raise NotImplementedError until implemented. section 4.3.1

Listing 4.5 Feature file for our BDD test
Feature: API /files

  Scenario: I get a 200 status code when we call /files
    When I make a GET request to the endpoint /files
    Then the status code should be 200

Use the feature-file listing as a compact reference for pytest-bdd's scenario structure. section 4.3.1

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Explains installation, directory layout, feature files, decorators, parsers, fixtures, and method limitations without deeper framework internals.
Examples
Uses a FastAPI /files endpoint, a GET request scenario, a status-code assertion, and an unsupported-method failure.
What it assumes
Assumes basic pytest and HTTP API testing, while teaching the pytest-bdd layout and scenario-to-step connection.
What you can do after
Apply it via pytest-bdd to express API behavior in feature files and execute it through reusable Python step definitions.
How it connects
Connects pytest with behavior-driven development, API testing, pytest fixtures, FastAPI TestClient, and continuous integration and delivery.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.