Generators¶
behave-gen can generate .feature files and step definitions from external
specifications: OpenAPI 3.x, Postman Collections, and Swagger 2.0.
from-openapi¶
Generate features and HTTP steps from an OpenAPI 3.x spec (YAML or JSON).
How it works¶
- Parse — The OpenAPI spec is parsed (YAML via
pyyaml, JSON via the standard library). Requires theopenapiextra for YAML files. - Build features — Each path produces a
.featurefile grouped by path segment. Each operation (GET, POST, PUT, DELETE, PATCH) becomes a scenario. - Build steps — When
--step-lib httpis passed, a concretehttp_steps.pymodule is generated alongside the features.
Output structure¶
gen/
features/
pets.feature # /pets endpoints
pets_petId.feature # /pets/{petId} endpoints
steps/
http_steps.py # HTTP step library
Filtering¶
Restrict generation to specific paths or methods:
behave-gen from-openapi spec.yaml \
--include-path /pets \
--include-method GET \
--include-method POST
Generated scenario example¶
For a GET /pets operation:
@api
Feature: Pets
Scenarios for Pets generated from Petstore YAML API.
Scenario: List all pets
When I send a GET request to "/pets"
Then the response status should be 200
The scenarios use the HTTP step library
patterns, so they work with the generated http_steps.py.
Options¶
| Option | Description |
|---|---|
SPEC |
Path to an OpenAPI 3.x spec (YAML or JSON). |
--out-dir |
Output directory for the generated project (default: gen). |
--step-lib |
Step library to bind (e.g. http). |
--tag |
Tag applied to all generated scenarios. |
--include-path |
Restrict to these paths (repeatable). |
--include-method |
Restrict to these HTTP methods (repeatable). |
from-postman¶
Generate features from a Postman Collection v2.1 JSON file.
How it works¶
- Parse — The Postman collection JSON is parsed. Each item (request) is extracted with its method, URL, name, and description.
- Build features — Items are grouped by Postman folder. Each folder
becomes a
.featurefile, each request becomes a scenario. - Build steps — When
--step-lib httpis passed, anhttp_steps.pymodule is generated.
Options¶
| Option | Description |
|---|---|
COLLECTION |
Path to a Postman Collection v2.1 JSON file. |
--out-dir |
Output directory for the generated project (default: gen). |
--step-lib |
Step library to bind (e.g. http). |
--tag |
Tag applied to all generated scenarios. |
from-swagger¶
Convert a Swagger 2.0 spec to OpenAPI 3.x in memory, then generate features and steps using the OpenAPI generator.
How it works¶
- Convert — The Swagger 2.0 spec is converted to OpenAPI 3.x in memory. Paths, operations, parameters, and responses are mapped.
- Build features — Same as
from-openapi. - Build steps — Same as
from-openapi.
Options¶
| Option | Description |
|---|---|
SPEC |
Path to a Swagger 2.0 spec (JSON). |
--out-dir |
Output directory for the generated project (default: gen). |
--step-lib |
Step library to bind (e.g. http). |
--tag |
Tag applied to all generated scenarios. |
Re-generating with update¶
When the source spec changes, re-run the generator or use behave-gen update:
This re-applies generated environment.py and step libraries, preserving any
previously added --kit or --data wiring.