Collections
AutoFixture can produce standalone collections (Create<string[]>(), CreateMany<int>(5)), assign a collection to a writable property with With, or append items to an ICollection<T> you already hold with AddManyTo.
Example scenario
You are testing code that sums a list of integers, reads tags from a writable list property, or needs an order with populated lines before your service accepts it.
Create arrays and lists
Create fills arrays and lists with anonymous elements:
var fixture = new Fixture();
var tags = fixture.Create<string[]>();
var scores = fixture.Create<List<int>>();
Assert.NotEmpty(tags);
Assert.NotEmpty(scores);
Assert.All(tags, tag => Assert.False(string.IsNullOrEmpty(tag)));
AutoFixture picks a default length based on RepeatCount.
CreateMany
When you need a specific number of distinct values, use CreateMany<T>(count):
var values = fixture.CreateMany<int>(5).ToList();
Assert.Equal(5, values.Count);
Assert.True(values.Distinct().Count() > 1);
CreateMany returns a lazy IEnumerable<T> — call .ToList() or .ToArray() when you need a fixed collection. Values are distinct by default for primitive types. See also Fixture and Create.
The Seed extensions package adds seeded overloads such as CreateMany("Item", 3) for readable string prefixes.
Assign collection properties with With
When a property is assignable, create the collection and assign it with With:
public sealed class TagBag
{
public List<string> Labels { get; set; } = [];
}
var tags = fixture.Build<TagBag>()
.With(b => b.Labels, fixture.CreateMany<string>().ToList())
.Create();
Assert.Equal(fixture.RepeatCount, tags.Labels.Count);
For arrays:
public sealed class ScoreBatch
{
public int[] Values { get; set; } = [];
}
var batch = fixture.Build<ScoreBatch>()
.With(b => b.Values, fixture.CreateMany<int>().ToArray())
.Create();
Assert.Equal(fixture.RepeatCount, batch.Values.Length);
AddManyTo on an existing collection
AddManyTo appends anonymous items to an ICollection<T> you already have. By default it adds RepeatCount items:
var lines = new List<OrderLine>();
fixture.AddManyTo(lines);
Assert.Equal(fixture.RepeatCount, lines.Count);
Pass an explicit count or factory when you need control:
fixture.AddManyTo(lines, 3);
fixture.AddManyTo(lines, () => fixture.Build<OrderLine>()
.With(l => l.Quantity, 1u)
.Create());
Read-only collection properties
Some types expose a collection through a read-only property — the list is created in the constructor and cannot be replaced with With. Plain Create<Order>() leaves OrderLines empty unless you enable ReadonlyCollectionPropertiesBehavior:
var fixture = new Fixture();
fixture.Behaviors.Add(new ReadonlyCollectionPropertiesBehavior());
var order = fixture.Create<Order>();
Assert.Equal(fixture.RepeatCount, order.OrderLines.Count);
The behavior calls Add on read-only properties that implement ICollection<T>. Item count follows RepeatCount.
For a single order without enabling the behavior globally:
var order = fixture.Build<Order>()
.Do(o => fixture.AddManyTo(o.OrderLines))
.Create();
Assert.Equal(fixture.RepeatCount, order.OrderLines.Count);
To add one specific item, use Do — see Refactoring a test.
How it works
Create<T[]>()/Create<List<T>>()— standalone collections filled in one callCreateMany<T>(count)— lazy sequence of distinct specimensWith(..., CreateMany<T>().ToList())— assigns a populated collection during the buildAddManyTo(collection)— appends items to an existingICollection<T>ReadonlyCollectionPropertiesBehavior— fills read-onlyICollection<T>properties viaAddBuild+Do+AddManyTo— populate one specimen's read-only collection without a global behavior
When nested graphs fail validation, combine these with Specimen graphs and Build DSL.