Skip to main content

Pytest plugin with Faker class and fixture

Project description

pytest-datafaker

Python Version License

A pytest plugin for simple generation of test data using the Mimesis library. Provides a convenient data_faker fixture for creating realistic fake data in your tests.

🚀 Features

  • 🌍 Multilingual Support - 30+ languages and locales
  • 🔐 Controlled Generation - ability to set seed for reproducible results
  • 🧵 Thread Safety - uses Singleton pattern with multithreading support
  • ⚙️ Flexible Configuration - via pyproject.toml or command line parameters
  • 📦 Minimal Dependencies - only pytest and mimesis

📦 Installation

pip install pytest-datafaker

Or with Poetry:

poetry add pytest-datafaker

🎯 Quick Start

Basic Example

import pytest

def test_user_creation(data_faker):
    """Test with fake data using default EN locale."""
    name = data_faker.api.person.full_name()
    email = data_faker.api.person.email()
    phone = data_faker.api.person.telephone()
    
    assert name
    assert email
    assert "@" in email
    assert phone

Using Different Locales

from mimesis.enums import Locale

def test_with_russian_data(data_faker):
    """Generate data in Russian using locale dictionary."""
    if Locale.RU in data_faker.locale:
        name_ru = data_faker.locale[Locale.RU].person.full_name()
        address_ru = data_faker.locale[Locale.RU].address.address()
        
        assert name_ru
        assert address_ru

Accessing Default API

The api field provides direct access to the English (default) locale:

def test_default_locale(data_faker):
    """Access methods through the default api field."""
    # Both are equivalent for EN locale
    data_faker.api.person.full_name()

⚙️ Configuration

Via pyproject.toml

Add a [tool.pytest_datafaker] section to your pyproject.toml file:

[tool.pytest_datafaker]
seed = 42
locales = ["en", "ru", "de", "fr"]

Via Command Line

pytest --datafaker-seed 123 tests/

📚 Available Methods

The data_faker fixture provides access to Mimesis methods through two fields:

  • data_faker.api - Generic instance for the default English locale
  • data_faker.locale - Dictionary of Generic instances for configured locales

Examples

def test_person_data(data_faker):
    """Methods for generating person data."""
    data_faker.api.person.full_name()
    data_faker.api.person.first_name()
    data_faker.api.person.last_name()
    data_faker.api.person.email()
    data_faker.api.person.telephone()
    data_faker.api.person.gender()

def test_address_data(data_faker):
    """Methods for generating address data."""
    data_faker.api.address.address()
    data_faker.api.address.street_name()
    data_faker.api.address.city()
    data_faker.api.address.country()
    data_faker.api.address.postal_code()

def test_internet_data(data_faker):
    """Methods for generating internet data."""
    data_faker.api.internet.url()
    data_faker.api.internet.ip_v4()
    data_faker.api.internet.username()
    data_faker.api.internet.password()

def test_datetime_data(data_faker):
    """Methods for generating dates and times."""
    data_faker.api.datetime.datetime()
    data_faker.api.datetime.date()
    data_faker.api.datetime.time()
    data_faker.api.datetime.year()

For a complete list of available methods, see the Mimesis documentation.

🔧 Features

Thread Safety

DataFaker uses the Singleton pattern with locking to ensure thread safety:

from pytest_datafaker import DataFaker
from pytest_datafaker.config import DataFakerConfig

config = DataFakerConfig(seed=42)
faker1 = DataFaker(config)
faker2 = DataFaker(config)

assert faker1 is faker2  # Same instance

Class Structure

The DataFaker class has the following structure:

  • api - Generic instance for the default English locale. Use this for basic/default locale access
  • locale - Dictionary mapping Locale to Generic instances for all configured locales. Use this when you need data in specific languages
def test_class_structure(data_faker):
    """Understanding DataFaker class structure."""
    # Default EN locale
    data_faker.api.person.full_name()
    
    # Access other configured locales through the locale dictionary
    from mimesis.enums import Locale
    if Locale.RU in data_faker.locale:
        data_faker.locale[Locale.RU].address.address()

Adding Custom Locales

from mimesis.enums import Locale

def test_with_custom_locale(data_faker):
    """Add additional locale during test."""
    data_faker.add_locale(Locale.DE)  # Add German language
    
    name_de = data_faker.locale[Locale.DE].person.full_name()
    assert name_de

📋 Requirements

  • Python 3.12+
  • pytest >= 9.0.3
  • mimesis >= 19.1.0

🧪 Running tests

Short instructions for running tests locally using Poetry:

# install Poetry (if not already installed)
pip install --user poetry

# install project dependencies and create virtual environment
poetry install

# run all tests inside the Poetry environment
poetry run pytest -q

# run a specific test file
poetry run pytest tests/test_datafaker.py -q

📄 License

This project is licensed under the MIT License.

👨‍💻 Author

Oleg Burenkov - burenkov.oleg@gmail.com

🤝 Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.

📞 Support

If you have any questions or issues, please create an issue in the repository.

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_datafaker-1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

pytest_datafaker-1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest_datafaker-1.0.tar.gz.

File metadata

  • Download URL: pytest_datafaker-1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytest_datafaker-1.0.tar.gz
Algorithm Hash digest
SHA256 47bdc9f1d0ac67db2fbbebacf59a2f7003289126d168d10dabccf46f76accc4f
MD5 d0ac1576b15aacbfe57fcfd06cc86f33
BLAKE2b-256 cba70ce56526e73b62aa226c1c083b92f76cde8d2a212adf27e8b8af4c1195bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_datafaker-1.0.tar.gz:

Publisher: ci.yml on burenk0v/pytest-datafaker

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_datafaker-1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_datafaker-1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytest_datafaker-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 93fa8a4aca5ca37a5096eb0d081e7d4a750079faadff1753d8b1652f35537786
MD5 4c4e52632254c26c600c3281338ef651
BLAKE2b-256 af0b51d92a83887646d36b8bf2d5ffa1bbcdb1c67b3a63383ac9c6d4f05c8e19

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_datafaker-1.0-py3-none-any.whl:

Publisher: ci.yml on burenk0v/pytest-datafaker

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