Customizations

Register factories and Customize composers to control how AutoFixture creates specimens.

Customizations change how AutoFixture builds specific types. Use Register when you want to replace creation entirely. Use Customize<T> when you want to adjust how a type is built — the same Build DSL you use for one-off specimens also works here, but applies to every Create<T>() on the fixture.

Example scenario

You need every ClientDto in a test to use a known age, or you need a type with multiple constructors to always be built a certain way.

Example

Register — replace the factory for a type:

var fixture = new Fixture();
fixture.Register(() => new ClientDto { Name = "registered", Age = 30 });

var client = fixture.Create<ClientDto>();

Assert.Equal("registered", client.Name);
Assert.Equal(30, client.Age);

Customize with With — fixture-wide default for one property:

var fixture = new Fixture();
fixture.Customize<ClientDto>(composer => composer.With(dto => dto.Age, 21));

var client = fixture.Create<ClientDto>();

Assert.Equal(21, client.Age);
Assert.False(string.IsNullOrEmpty(client.Name));

Other properties stay anonymous; only Age is pinned for every Create<ClientDto>().

Customize with Without — leave a property at its default:

fixture.Customize<ClientDto>(composer => composer.Without(dto => dto.Age));

var client = fixture.Create<ClientDto>();

Assert.Equal(default, client.Age);
Assert.False(string.IsNullOrEmpty(client.Name));

Customize with OmitAutoProperties — constructor only, no auto-filled properties:

fixture.Customize<ClientDto>(composer => composer.OmitAutoProperties());

var client = fixture.Create<ClientDto>();

Assert.Equal(string.Empty, client.Name);
Assert.Equal(default, client.Age);

Customize with FromFactory — always construct the type your way:

fixture.Customize<MultiCtorProduct>(composer =>
    composer.FromFactory(() => new MultiCtorProduct("widget", 9.99m)));

var product = fixture.Create<MultiCtorProduct>();

Assert.Equal("widget", product.Name);
Assert.Equal(9.99m, product.Price);

AutoFixture skips its normal constructor selection and calls your factory every time. Use this when you need a specific constructor, or when creation depends on objects you control.

Customize with FromSeed — map AutoFixture's seed to a value:

fixture.Customize<int>(composer => composer.FromSeed(seed => seed * 10));

var first = fixture.Create<int>();
var second = fixture.Create<int>();

Assert.Equal(0, first);
Assert.Equal(first, second);

Bare Create<int>() uses default(int) as the seed, so both calls return 0. With Seed extensions, Create(5) returns 50. Ints inside object graphs often use string seeds (property or parameter names), so this rule may not apply there — the default random generator still runs.

FromFactory vs FromSeed

FromFactoryFromSeed
InputYour factory — no seedA seed from AutoFixture's pipeline
ControlYou own construction entirelyYou transform seed → value
Typical usePick a constructor, wire known dependenciesRepeatable values from seed context
Close toRegister (always your code)Seed-aware rule, not full replacement

FromFactory(() => new OrderLine(product)) is right when you must pass a specific product. FromSeed(seed => seed * 10) is right when you want a deterministic function of whatever seed AutoFixture supplies for that request.

You can chain composer methods — for example With and Without together — the same way you chain fixture.Build<T>().

Customize vs Build

Customize<T>(...)Build<T>().Create()
ScopeEvery Create<T>() on the fixtureOne specimen
Customizations on fixtureAppliedBypassed
Typical useShared defaults across a test class or fixture factoryOne test with specific constraints

For graph constraints (Do, FromFactory) or a single pinned object, prefer Build. For team-wide or test-class defaults, prefer Customize or an ICustomization implementation.

How it works

  • Register<T>(Func<T>) — replaces the default factory for T
  • Customize<T>(Func<IPostprocessComposer<T>, ISpecimenBuilder>) — wraps the composer pipeline for T; pass the same methods as Build DSL: With, Without, Do, FromFactory, FromSeed, OmitAutoProperties, and others
  • FromFactory — ignores normal creation and calls your factory for every request for T
  • FromSeed — maps a seed to a value when the request carries a compatible seed (see the FromSeed example above)

See also Register, Freeze, and Inject and Customizations catalog.

Next steps

API