polyfactory

Generate typed mock data factories and pytest fixtures for Python model classes.

Updated Aug 17, 2026
One-click install
npx skills add https://github.com/renjianguo666/litecms --skill polyfactory-renjianguo666
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: polyfactory
Source: https://github.com/renjianguo666/litecms/tree/main/.agents/skills/polyfactory
Command: npx skills add https://github.com/renjianguo666/litecms --skill polyfactory-renjianguo666

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires polyfactory, pytest, faker, and includes references (resource) components.

What problem does it solve? Writing tests requires realistic, validation-passing mock data, but hand-crafting fixtures for every Pydantic model, dataclass, msgspec Struct, or SQLAlchemy entity is repetitive and drifts out of sync as models evolve. This Skill guides the creation of polyfactory-based factories that introspect model annotations and constraints to produce fully-populated, valid instances automatically. ## Core Features & Use Cases - Backend-matched factory bases: Pick the correct factory base per model type — ModelFactory for Pydantic, DataclassFactory for dataclasses, MsgspecFactory for msgspec Structs, AttrsFactory for attrs, TypedDictFactory, and SQLAlchemyFactory for ORM models — so generated data respects field constraints and validators. - Field customization and determinism: Pin literal values, re-evaluate callables per build with Use, derive fields with PostGenerated, and control randomness via random_seed, faker, and allow_none_optionals. - Pytest integration: Turn factories into fixtures with @register_fixture, wire cross-model relationships with Fixture(...) or set_as_default_factory_for_type, and fan out parametrized tests over union branches with coverage(). - Use Case: In a Litestar project, register a MsgspecFactory for a request DTO, inject the fixture into an AsyncTestClient handler test, and POST a validation-passing payload without writing any inline fake data. ## Quick Start Create a polyfactory factory for my Order dataclass and register it as a pytest fixture for my test suite.

Frequently Asked Questions about polyfactory

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I generate mock data for Pydantic models in pytest?

Subclass ModelFactory from polyfactory.factories.pydantic_factory, set __model__ to your Pydantic model, and call .build() to get a fully-populated instance. Decorate the class with @register_fixture to also expose it as a pytest fixture.

Which polyfactory factory base should I use for msgspec Structs?

Use MsgspecFactory from polyfactory.factories.msgspec_factory for msgspec.Struct models. Using ModelFactory on a Struct falls back to generic introspection and can produce values that violate msgspec.Meta constraints.

How do I make polyfactory generate deterministic test data?

Set __random_seed__ to an integer on the factory class so every run produces identical instances. For finer control, assign a seeded Faker instance via __faker__, and use a distinct seed per factory to avoid collisions.

Can polyfactory factories be used as pytest fixtures?

Yes, decorate the factory class with @register_fixture from polyfactory.pytest_plugin. The fixture is exposed under the snake-cased class name and yields the factory class itself, so tests call .build() or .batch() on it.

When should I not use polyfactory for test data?

Avoid polyfactory for property-based testing, where Hypothesis is the right tool for exploring input spaces, and for production data seeding, which should use a deterministic seeder script. Hand-curated end-to-end smoke tests are also clearer with inline JSON.

How do I test all variants of a discriminated union with polyfactory?

Call coverage() on the factory to get one instance per Union or Optional branch, then feed the list into pytest.mark.parametrize. This exercises every dispatch path of a polymorphic DTO with a single test function.