Skip to main content

Convert Postman Collection v2.1 JSON files into executable pytest test suites

Project description

postman2pytest

CI PyPI Python License: MIT

Convert a Postman Collection v2.1 JSON file into a ready-to-run pytest test suite. One command.

📖 Read the article on Dev.to

postman2pytest --collection my_api.json --out tests/test_api.py
BASE_URL=https://api.example.com pytest tests/test_api.py -v

Why

Postman collections document your API. postman2pytest turns that documentation into executable regression tests that run in CI. No manual rewriting, no drift.

Install

pip install postman2pytest

Or from source:

git clone https://github.com/golikovichev/postman2pytest
cd postman2pytest
pip install -e .

Usage

postman2pytest \
  --collection data/my_api.postman_collection.json \
  --out generated_tests/test_api.py

Then run the generated tests:

BASE_URL=https://staging.example.com pytest generated_tests/test_api.py -v

Options

Flag Required Description
--collection Path to Postman Collection v2.1 JSON
--out Output path for generated pytest file
--base-url Tip printed after generation (does not override env var)

How It Works

  1. Parse: reads the Postman Collection JSON, flattens nested folders into a flat request list
  2. Extract: captures method, URL, headers, body, and expected status from pm.response.to.have.status() test scripts
  3. Generate: renders a Jinja2 template into a .py file with one def test_*() per request

Variable substitution

Postman variables {{base_url}} become ENV_base_url in the URL, resolved at runtime via the BASE_URL environment variable.

Generated output example

Given a Postman request GET {{base_url}}/api/v1/users with a test asserting status 200, the output is:

def test_get_users():
    """GET ENV_base_url/api/v1/users"""
    url = f"{BASE_URL}/api/v1/users"
    headers = {}
    response = requests.get(url, headers=headers)
    assert response.status_code == 200, (
        f"Expected 200, got {response.status_code}: {response.text[:200]}"
    )

Supported features

  • ✅ Postman Collection v2.1 (v2.0 accepted with a warning)
  • ✅ Nested folders → flattened with folder prefix in test name
  • ✅ GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
  • ✅ Request headers (disabled headers excluded)
  • ✅ Raw JSON body
  • ✅ Expected status from pm.response.to.have.status(N) test scripts
  • ✅ Falls back to 200 when no status assertion found
  • ✅ Malformed items skipped with a warning. Rest of collection still generated

Running tests

pip install pytest
pytest tests/ -v

Contributing

See CONTRIBUTING.md.

Changelog

See CHANGELOG.md for release notes.

License

MIT. See LICENSE.

Project details


Download files

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

Source Distribution

postman2pytest-1.0.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

postman2pytest-1.0.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file postman2pytest-1.0.1.tar.gz.

File metadata

  • Download URL: postman2pytest-1.0.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for postman2pytest-1.0.1.tar.gz
Algorithm Hash digest
SHA256 56d7c5d7a867910b31476aeeee2508a51611f94eb036c76d08289ddbca965737
MD5 ce6d77738af409a3dfb57d2fcb98ed3e
BLAKE2b-256 ee712dc96593b8880f3b5b79a562a0ae1132bc8bd5ab629b1c1de1e601a521f6

See more details on using hashes here.

File details

Details for the file postman2pytest-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: postman2pytest-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for postman2pytest-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 365c6358b98913e08284fab2f390376d448eeecf4c9acce153f5b8fc5c9647b5
MD5 8b4fb0f4020b0dee85124fc2f50f9117
BLAKE2b-256 9fba6529a0ab146c797d69cff5000d4dbb6695f76b90dbd66297ac5fe5395830

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page