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.2.tar.gz (15.9 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.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: postman2pytest-1.0.2.tar.gz
  • Upload date:
  • Size: 15.9 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.2.tar.gz
Algorithm Hash digest
SHA256 06e7db82bbdd6631c7004ad902aae5b9596bedaec99835996e204405031b6018
MD5 8a9aff8e0359d34b3332f33652705320
BLAKE2b-256 e5474fbb6db1c6fecd3fbeaa7a9ff21e8c8361bf83f1906da2233b47cde93843

See more details on using hashes here.

File details

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

File metadata

  • Download URL: postman2pytest-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.9 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 894e36cc703fb63697a1c845646460e50a320927850ab1899ee115872e172f59
MD5 93da2278d5eab47b6c48a3ffeb93506f
BLAKE2b-256 98ac1bc8cfc9fa7c7aa4c83ac0f233bdc3d2ac952d3dc778acbfc106fb925137

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