Skip to content

Fabricator

Typed test data factories for Go. Say what matters to the test; let the rest be generated.
type Person struct {
ID int
FirstName string
LastName string
Email string
}
firstName := fabricator.FieldOf[Person, string]("FirstName")
id := fabricator.FieldOf[Person, int]("ID")
factory := fabricator.New(Person{},
fabricator.Value(firstName, "Moishe"),
fabricator.Field(id, func(ctx fabricator.BuildContext) int {
return ctx.Iteration + 1
}),
)
person := factory.Build()
people := factory.Batch(10)

Typed field references

FieldOf[Person, string]("FirstName") checks at construction that the field exists, is exported, and accepts the value type. A typo fails immediately, not three assertions later.

Generated by default

Unconfigured fields are filled by go-faker. Configure only what the test is actually about.

Reproducible when you need it

Seed pins generation so a test that fails on generated data fails the same way next run. WithoutFaker drops generation entirely for exact fixtures.

Factories from factories

Extend derives a variant without restating the base. Subfactories nest whole factories into struct, pointer, and slice fields.

Errors, not just panics

Every panicking method has an error-returning twin: BuildE, BatchE, CreateE, CreateBatchE.

Persistence built in

Give a factory a PersistenceHandler and Create builds and saves in one step, with a context.

Terminal window
go get github.com/Goldziher/fabricator/v2

Fabricator requires Go 1.26 or newer.