Skip to main content

SwaggerForge

Automatic pytest test generation from OpenAPI (Swagger) specifications.

SwaggerForge is a Python library and command-line tool that reads an OpenAPI specification and generates ready-to-run pytest test files - one per resource covering positive, negative, boundary, and boolean scenarios grounded in established test-design techniques.

Features

  • Reads OpenAPI 3.x specifications in JSON or YAML
  • Resolves $ref references automatically
  • Generates one pytest file per resource tag
  • Produces six scenario types per endpoint where applicable:
    • Positive >>> valid request, expects a 2xx response and validates the response schema
    • Missing required field >>> omits a required field, expects 400
    • Wrong data type >>> sends a mistyped field, expects 400/422
    • Nonexistent resource >>> requests an unlikely identifier, expects 404
    • Boundary values >>> tests values at and just beyond declared numeric/length limits (Boundary Value Analysis)
    • Boolean coverage >>> exercises both true and false for boolean fields
  • Deterministic output: the same specification always produces identical tests
  • Generated files use session-scoped pytest fixtures and run with no manual edits
  • Optional swaggerforge.toml config file for project-level defaults
  • Authentication support: bearer, API key, and basic schemes, with environment-variable substitution for secrets

Requirements

  • Python 3.10 or newer

Installation

pip install swaggerforge

Usage

Generate tests from a specification, pointing at the base URL of the API under test:

swaggerforge generate --spec swagger.json --url http://localhost:8080

This reads swagger.json, writes one test_<resource>.py file per resource tag into the output directory (default: tests_generated/), and the files can be run immediately:

pytest tests_generated

Options

Option Description Default
--spec Path to the OpenAPI specification (JSON or YAML) (required)
--url Base URL of the API under test (required unless in config)
--output Directory for the generated test files tests_generated
--config Path to a configuration file ./swaggerforge.toml if present

Configuration

Options that stay the same across runs can be kept in a swaggerforge.toml file instead of being passed on the command line. The file is picked up automatically from the directory where the tool is run, or an explicit path can be given with --config.

# swaggerforge.toml
base_url = "http://localhost:8080"
output_dir = "tests_generated"
timeout = 30
Key Type Effect
base_url string Base URL of the API; makes --url optional
output_dir string Directory for generated files
timeout integer Embeds timeout=<n> into every generated HTTP request call

Values given on the command line always take precedence over the config file. Without a timeout, generated tests place no time limit on requests - setting one makes test runs fail fast when the API is unreachable.

Authentication

When a specification declares security requirements, SwaggerForge embeds the matching authentication headers into the generated tests. Credentials are supplied through the config file, never taken from the specification itself.

# swaggerforge.toml
[auth.bearer]
token = "${API_TOKEN}"

[auth.api_key]
value = "${API_KEY}"

[auth.basic]
username = "${API_USER}"
password = "${API_PASSWORD}"

Three scheme kinds are supported, matching the OpenAPI security scheme types:

Section Applies to Generated header
[auth.bearer] http bearer, and oauth2 (pre-obtained token) Authorization: Bearer <token>
[auth.api_key] apiKey in header the header named by the specification
[auth.basic] http basic Authorization: Basic <base64>

For an API key, the header name comes from the specification's scheme definition; the config supplies only the secret value.

Environment variables. Any auth value may reference an environment variable with ${VAR} syntax, so secrets stay out of the config file. A referenced variable that is not set is an error.

Unconfigured or unsupported schemes. If a specification requires a scheme whose credentials are not configured, SwaggerForge prints a warning and generates the tests without that header, rather than failing. Schemes that are not yet supported - API keys in a query string or cookie, and OpenID Connect - are likewise skipped with a warning.

How it works

SwaggerForge runs a six-stage pipeline: the specification is validated, parsed into an internal model (with $refs resolved), turned into test scenarios based on test-design techniques, rendered into pytest code via templates, and written to per-resource files.

Limitations

  • Targets OpenAPI 3.x with JSON request bodies
  • Authentication covers bearer, API key (header), and basic schemes; API keys in query or cookie, full OAuth2 flows, and OpenID Connect are not yet handled
  • Boundary tests require the specification to declare numeric/length constraints

License

This project is licensed under the MIT License - see the LICENSE file.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swaggerforge-0.3.0.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swaggerforge-0.3.0-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file swaggerforge-0.3.0.tar.gz.

File metadata

  • Download URL: swaggerforge-0.3.0.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for swaggerforge-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6016c87d7cf54ad1b7fcbb5f9f0233abe4ec43a6ab283ef4dbb8a6f617a9bd4d
MD5 a27d5a16f9fe8ca1a5729f33b507c20a
BLAKE2b-256 6430329a4db1e8ceac0503372c8f1007f60edca0e2725aef568966cf8614414b

See more details on using hashes here.

File details

Details for the file swaggerforge-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: swaggerforge-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for swaggerforge-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f99db3a897b8f44fdfb2f66c5919bff0b382fdfa8a2ce40adfafe005cb39a09
MD5 f974a377959c467a8bfadcd2092afdec
BLAKE2b-256 a06ccea0cd86f22a4867739ef2bb76b8ab5b558278f0b79c18a84aafa5e97b9c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page