Skip to main content

Add your description here

Project description

Test Scenarios

Test Scenarios is a Python library designed to make integration testing with MongoDB effortless and maintainable. It enables you to define reusable data templates and quickly build test scenarios, so you can focus on writing meaningful tests instead of boilerplate setup code.

codecov PyPI version

Why use Test Scenarios?

  • Rapid scenario creation: Define templates once, reuse them across tests, and override only what you need.
  • Consistent test data: Ensure your integration tests always start with predictable, isolated data.
  • Flexible configuration: Use environment variables or pytest config files to adapt to any project setup.
  • Seamless pytest integration: Built for pytest, with fixtures and helpers ready to use.
  • Clean and readable tests: Keep your test code focused on logic, not data plumbing.

Installation

Install with pip:

pip install pytest-scenarios

Or with uv:

uv add pytest-scenarios --dev

Or with Poetry:

poetry add pytest-scenarios --group dev

What are templates?

Templates are Python dictionaries representing MongoDB documents with default values. Each template module matches a MongoDB collection.

See tests/templates for examples like customers, orders, and products.

Example template:

# tests/templates/orders.py

# Each template should be assigned to a `TEMPLATE` var
TEMPLATE = {
    "id": "123456789abcdef01234567",
    "customer_id": "customer_001",
    "items": [
        {"product_id": "product_001", "quantity": 2, "price": 19.99},
        {"product_id": "product_002", "quantity": 1, "price": 9.99},
    ],
    "tax": 0.15,
}

Configuration

Configure the library using environment variables or pytest config files.

MongoDB Connection

Set your MongoDB URI and database name:

# Environment variables
DB_URL=mongodb://localhost:27017
DB_NAME=test_db

Or in pyproject.toml:

[tool.pytest.ini_options]
db-url="mongodb://localhost:27017"
db-name="test_db"

Or in pytest.ini:

[pytest]
db-url=mongodb://localhost:27017
db-name=test_db

Templates Path

Specify where your templates live:

# Environment variable
TEMPLATES_PATH=tests/templates

Or in config files:

[tool.pytest.ini_options]
templates-path="tests/templates"
[pytest]
templates-path=tests/templates

Quickstart

Get started in three steps:

  1. Configure your database and templates path as shown above.
  2. Create your templates in the configured templates-path.
  3. Write your test using the scenario builder fixture:
def test_example(
    scenario_builder: ScenarioBuilder, db: Database
):
    """
    Test that the scenario is created correctly.
    This example creates 2 customers and 2 orders, overriding template values.
    """
    inserted_ids_by_collection = scenario_builder.create(
        {
            "customers": [
                {"name": "Alice", "status": "inactive", "email": "alice@test.com"},
                {"name": "Louis", "age": 25, "email": "louis@test.com"},
            ],
            "orders": [
                {
                    "id": "order_001",
                    "items": [
                        {"price": 19.99, "product_id": "book_123", "quantity": 1}
                    ],
                },
                {
                    "id": "order_002",
                    "items": None,
                    "tax": 0.2,
                },
            ],
        }
    )
    for collection_name, inserted_ids in inserted_ids_by_collection:
        assert len(inserted_ids) == 2, collection_name

Check out generated documents in:

Example Use Cases

  • Integration tests for APIs and services using MongoDB
  • End-to-end tests requiring complex, multi-collection data setups
  • Rapid prototyping of test data for new features

Contributing

Contributions welcome. Please add tests for new features and follow the project's coding standards.

License

MIT

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

pytest_scenarios-0.0.8.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

pytest_scenarios-0.0.8-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest_scenarios-0.0.8.tar.gz.

File metadata

  • Download URL: pytest_scenarios-0.0.8.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_scenarios-0.0.8.tar.gz
Algorithm Hash digest
SHA256 48620c12a2b6cc4ad080c15313cbfe3a91a479c4b1d4969b126a3a06373912a5
MD5 6405b1b459e54760a1768125e5acf734
BLAKE2b-256 ff8a86f797d4f258d79aafa002440df53373e5033c3c7cd1027119dda956d714

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_scenarios-0.0.8.tar.gz:

Publisher: release.yaml on carlosvin/pytest-scenarios

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_scenarios-0.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_scenarios-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6aacacc338a0427ba9fe102a4f3148b263ad1b11820050eb5b77dc8f2dd9a8ad
MD5 6cb3f96cac262af8a86e806b798d2fda
BLAKE2b-256 531239d31653b4563bde2c8525b634506688657fe13ed282935f08845f461c1e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_scenarios-0.0.8-py3-none-any.whl:

Publisher: release.yaml on carlosvin/pytest-scenarios

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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