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
  • Comprehensive authentication: bearer, basic, API key (header/query/cookie), multiple-scheme AND/OR requirements, and OAuth2 / OpenID Connect (optional extra), 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 configures the generated tests to authenticate. Credentials are supplied through the config file; the specification decides which scheme and where each value goes, and the config supplies the secret values.

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

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

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

[auth.oauth2]
client_id = "${OAUTH_CLIENT_ID}"
client_secret = "${OAUTH_CLIENT_SECRET}"
scope = "read write"

Generated tests build a session-scoped requests.Session that carries the resolved authentication, so every request is authenticated without repeating credentials per call.

Supported schemes

Config section OpenAPI scheme How it is applied
[auth.bearer] http bearer Authorization: Bearer <token> header
[auth.api_key] apiKey in header, query, or cookie the name and location declared by the specification
[auth.basic] http basic Authorization: Basic <base64> header
[auth.oauth2] oauth2 client credentials, openIdConnect a token fetched at runtime (see below)

For an API key, both the name and the location (header, query, or cookie) come from the specification; the config supplies only the secret value.

Multiple schemes

OpenAPI can require several schemes at once (AND) or offer alternatives (OR). SwaggerForge honours both: it uses the first alternative whose credentials are fully configured, combining every scheme in an AND group. When endpoints in one file need different authentication, the differing ones override the session per request.

OAuth2 and OpenID Connect

OAuth2 and OIDC support is an optional extra, installed with:

pip install swaggerforge[oauth2]

For the client credentials flow, generated tests fetch a fresh access token from the specification's tokenUrl at runtime (and refresh it on expiry), so no token is ever embedded at generation time. OpenID Connect schemes work the same way, discovering the token endpoint at runtime from the specification's openIdConnectUrl. Both need client_id and client_secret in [auth.oauth2].

Interactive flows (OAuth2 authorization code) require a human to log in through a browser and so cannot be fully automated. For these, SwaggerForge generates a clearly marked placeholder in the session fixture where you paste a token obtained manually.

Environment variables and missing credentials

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. If a specification requires a scheme whose credentials are not configured, SwaggerForge prints a warning naming the scheme and generates the tests without that authentication rather than failing.

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, basic, API key (header/query/cookie), OAuth2 client credentials, and OpenID Connect; interactive OAuth2 flows (authorization code) generate a placeholder for a manually obtained token, as they require a browser-based login that cannot be automated
  • Boundary tests require the specification to declare numeric/length constraints

License

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

Metadata

Release files for swaggerforge 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swaggerforge 0.3.1
File Size Uploaded Provenance Actions
swaggerforge-0.3.1.tar.gz 36.4 kB Without provenance Details for swaggerforge-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swaggerforge 0.3.1
File Interpreter ABI Platform Actions
swaggerforge-0.3.1-py3-none-any.whl Python 3 none any Details for swaggerforge-0.3.1-py3-none-any.whl

Total release size: 60.4 kB

Release files / swaggerforge-0.3.1.tar.gz

Download URL swaggerforge-0.3.1.tar.gz
Size 36.4 kB
Tags Source
SHA-256 checksum
How to use checksums
389b5b32d23f947aac16d495ec8bfc883150813ce06e3ec0cd0567e74f9dcccc
BLAKE2b-256 checksum
How to use checksums
0f9daf8b871c8bb2c9d90e199a122444768460a8042960b5adb11d9c32eeeb25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / swaggerforge-0.3.1-py3-none-any.whl

Download URL swaggerforge-0.3.1-py3-none-any.whl
Size 24.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de277163bc35128af37beaea03d8f99c8c123f646de65919980bfa75a05cb020
BLAKE2b-256 checksum
How to use checksums
9e2643dcff3c8909924e5433dd069a17d90fb258ba7e9217c114a7a6e980c5b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Security for swaggerforge 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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