A Python library for generating mock data from dataclasses, Pydantic models, typed dicts, and other type-annotated structures.
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.
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.
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.
Simple and powerful factories for mock data generation
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
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.
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.
Allows overriding field values and using custom generation functions, enabling realistic test cases for edge scenarios without sacrificing automation, per the customizable factories feature.
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.
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.
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.
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.