Skip to main content

Deterministic test data generator from JSON schemas for consistent, repeatable testing

Project description

Test Data Generator

A deterministic test data generator that creates consistent, repeatable test data from JSON schemas. Perfect for writing reliable tests that produce the same results every time.

Features

  • Deterministic Generation - Same seed always produces identical data
  • 📝 Flexible Input - Accept schemas as file paths, JSON strings, or Python dicts
  • 🎯 Type-Rich - Support for strings, integers, floats, booleans, dates, emails, and UUIDs
  • 🔧 Constraint Support - Min/max ranges, enums, and length constraints
  • 🧪 Testing-First - Built specifically for creating consistent test fixtures
  • 🚀 Zero Dependencies - Uses only Python standard library (except dev tools)

Installation

pip install test-data-generator

Quick Start

from test_data_generator import generate_test_data

# Define your schema
schema = {
    "fields": {
        "user_id": {"type": "uuid"},
        "username": {
            "type": "string",
            "constraints": {"min": 5, "max": 15}
        },
        "email": {"type": "email"},
        "age": {
            "type": "integer",
            "constraints": {"min": 18, "max": 100}
        },
        "active": {"type": "boolean"}
    }
}

# Generate test data
data = generate_test_data(schema, count=10, seed=42)

# Same seed = same data every time!
data1 = generate_test_data(schema, count=5, seed=42)
data2 = generate_test_data(schema, count=5, seed=42)
assert data1 == data2  # ✓ Always true

Schema Format

Schemas define the structure and constraints for generated data:

{
  "fields": {
    "field_name": {
      "type": "<type>",
      "constraints": {
        "min": 0,
        "max": 100,
        "enum": ["value1", "value2"],
        "format": "iso"
      }
    }
  }
}

Supported Types

  • string - Random alphanumeric strings
  • integer - Random integers
  • float - Random floating-point numbers
  • boolean - Random true/false values
  • date - ISO format dates (2024-01-15)
  • email - Valid email addresses
  • uuid - UUID v4 identifiers

Constraints

Constraint Applies To Description
min string, integer, float, date Minimum value or length
max string, integer, float, date Maximum value or length
enum all types Pick from a fixed set of values
format date Date format: "iso" (default) or "timestamp"

Usage Examples

1. Using a Dictionary Schema

from test_data_generator import generate_test_data

schema = {
    "fields": {
        "name": {"type": "string"},
        "score": {"type": "integer", "constraints": {"min": 0, "max": 100}}
    }
}

data = generate_test_data(schema, count=5, seed=42)

2. Using a JSON String

schema_json = '''
{
    "fields": {
        "status": {
            "type": "string",
            "constraints": {"enum": ["active", "inactive", "pending"]}
        }
    }
}
'''

data = generate_test_data(schema_json, count=10)

3. Using a File Path

Create user_schema.json:

{
  "fields": {
    "user_id": { "type": "uuid" },
    "email": { "type": "email" },
    "created_at": { "type": "date" }
  }
}

Then use it:

data = generate_test_data("user_schema.json", count=20, seed=123)

4. Advanced: Using DataGenerator Class

For more control, use the DataGenerator class directly:

from test_data_generator import DataGenerator

schema = {
    "fields": {
        "value": {"type": "integer"}
    }
}

generator = DataGenerator(schema, seed=42)

# Generate batch 1
batch1 = generator.generate(5)

# Reset to regenerate same data
generator.reset_seed()
batch2 = generator.generate(5)

assert batch1 == batch2  # Same data again

# Or use a different seed
generator.reset_seed(seed=99)
batch3 = generator.generate(5)

assert batch1 != batch3  # Different data

Testing with Deterministic Data

The key benefit is writing tests with predictable data:

import pytest
from test_data_generator import generate_test_data

@pytest.fixture
def test_users():
    """Generate consistent test users for every test run."""
    schema = {
        "fields": {
            "user_id": {"type": "uuid"},
            "email": {"type": "email"},
            "age": {"type": "integer", "constraints": {"min": 18, "max": 65}}
        }
    }
    return generate_test_data(schema, count=10, seed=42)

def test_user_filtering(test_users):
    # test_users will be the SAME every time this test runs
    assert len(test_users) == 10
    # Write assertions based on the known deterministic data

Complex Schema Example

schema = {
    "fields": {
        "order_id": {"type": "uuid"},
        "customer_email": {"type": "email"},
        "order_date": {
            "type": "date",
            "constraints": {
                "min": "2024-01-01",
                "max": "2024-12-31"
            }
        },
        "status": {
            "type": "string",
            "constraints": {
                "enum": ["pending", "shipped", "delivered", "cancelled"]
            }
        },
        "total": {
            "type": "float",
            "constraints": {"min": 10.0, "max": 5000.0}
        },
        "is_priority": {"type": "boolean"}
    }
}

orders = generate_test_data(schema, count=100, seed=42)

Development

# Install in development mode
pip install -e .

# Run tests
pytest tests/ -v

# Run linter
ruff check src/

Requirements

  • Python 3.14+
  • No runtime dependencies (pure stdlib)
  • Dev dependencies: pytest, ruff

Architecture

  • load_schema.py - Schema loading from files/strings
  • schema.py - Schema validation and utilities
  • generators.py - Type-specific value generators
  • generator.py - Core DataGenerator class with seeding
  • __init__.py - Public API: generate_test_data() function

Why Deterministic?

Deterministic test data generation ensures:

  • ✓ Tests are reproducible across different machines
  • ✓ Failed tests can be debugged with the exact same data
  • ✓ CI/CD pipelines get consistent results
  • ✓ Team members see identical test behavior
  • ✓ Easier to reason about test failures

License

MIT 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

test_data_generator-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

test_data_generator-0.1.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file test_data_generator-0.1.0.tar.gz.

File metadata

  • Download URL: test_data_generator-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Darwin/24.6.0

File hashes

Hashes for test_data_generator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 109446775245de3d00fca2fbaef1cdc9188f665291b3501d667529a3e2272608
MD5 b092a338f29424c2ae77d2b5597d35de
BLAKE2b-256 cbd12c5c06fb2cba04ef7546715daaad10db19d5c5f0d3ee2de6e525751087e1

See more details on using hashes here.

File details

Details for the file test_data_generator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for test_data_generator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22dc5b1b1672ce7364da643fa2e78f36fbb674cebf47c8e5098aea4a8681a739
MD5 b89c07f0193fd69f96aa98b048a37c25
BLAKE2b-256 2850a024781ea4ee9b2912b21445e76b9d5de711a7f7909a95c78106c176bbff

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