Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Python
  3. polyfactory

polyfactory

MITPythonv3.3.0

A Python library for generating mock data from dataclasses, Pydantic models, typed dicts, and other type-annotated structures.

Visit WebsiteGitHubGitHub
1.5k stars113 forks0 contributors

What is polyfactory?

Polyfactory is a Python library that automatically generates mock data for testing purposes. It creates realistic test fixtures by parsing Python type hints from dataclasses, Pydantic models, typed dicts, and other structured types. This solves the problem of writing repetitive, hard-coded test data that must match complex application schemas.

Target Audience

Python developers and test engineers who need to generate consistent, schema-valid test data for applications using type-annotated data models, particularly those using Pydantic, dataclasses, or ORM/ODM frameworks.

Value Proposition

Developers choose Polyfactory because it dramatically reduces test boilerplate, ensures generated data always matches the application's type constraints, and supports a wide range of Python type systems through a unified, intuitive factory pattern.

Overview

Simple and powerful factories for mock data generation

Use Cases

Best For

  • Generating test fixtures for Pydantic models in FastAPI or Litestar applications
  • Creating mock database records for unit tests with Beanie or ODMantic
  • Populating development databases with realistic sample data
  • Testing data validation logic with varied, type-conforming inputs
  • Simplifying test setup for complex nested data structures
  • Ensuring test data consistency across Python dataclass and typed dict schemas

Not Ideal For

  • Projects primarily using SQLAlchemy ORM without Pydantic or dataclass wrappers
  • Teams that prefer hard-coded test data for simplicity and control over every value
  • Applications with data models that don't use Python type hints extensively
  • Environments requiring mock data generation via a graphical interface or CLI tool

Pros & Cons

Pros

Wide Model Support

Handles dataclasses, Pydantic v1/v2, typed dicts, msgspec structs, and MongoDB ODMs like Beanie, eliminating the need for separate mock libraries across different data structures in a project.

Automatic Type Inference

Leverages Python type annotations to generate valid data automatically, reducing boilerplate code and ensuring schema compliance with minimal configuration, as shown in the simple dataclass example.

Flexible Customization

Allows overriding field values and using custom generation functions, enabling realistic test cases for edge scenarios without sacrificing automation, per the customizable factories feature.

Batch Processing Efficiency

Includes a build_batch method for generating multiple instances efficiently, which is ideal for seeding test databases or creating large datasets, as highlighted in the key features.

Cons

Breaking Change History

Renamed from pydantic-factories in version 2.0 with architectural changes, which may require migration efforts for existing users and indicates potential future instability or compatibility issues.

Type Hint Dependency

Heavily relies on Python type hints; models without annotations or with complex custom types need extensive manual configuration, limiting out-of-the-box utility for legacy or loosely-typed codebases.

External Documentation Reliance

Documentation is hosted separately at polyfactory.litestar.dev, which can be less convenient than integrated docs and might suffer from accessibility or update delays compared to GitHub-based documentation.

Frequently Asked Questions

Quick Stats

Stars1,473
Forks113
Contributors0
Open Issues49
Last commit20 days ago
CreatedSince 2021

Tags

#fixtures#hacktoberfest#pydantic#mock-data#python#testing#test-automation#dataclasses#beanie#type-hints#test-factories

Built With

S
Sourcery
M
Mypy
P
Pydantic
P
Python
R
Ruff

Links & Resources

Website

Included in

Python290.8k
Auto-fetched 1 day ago

Related Projects

locustlocust

Write scalable load tests in plain Python 🚗💨

Stars27,880
Forks3,216
Last commit2 days ago
playwright-pythonplaywright-python

Python version of the Playwright testing and automation library.

Stars14,725
Forks1,177
Last commit21 days ago
pyautoguipyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.

Stars12,548
Forks1,425
Last commit1 year ago
hypothesishypothesis

The property-based testing library for Python

Stars8,675
Forks652
Last commit2 days ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub