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
