Data annotations

AutoFixture honors System.ComponentModel.DataAnnotations constraints when generating strings.

When properties use validation attributes, generators respect them.

Example scenario

ValidatedProfile has [Required] and [StringLength] constraints on its properties. Your test needs specimens that satisfy those rules so validation logic is not the thing being tested.

Example

var fixture = new Fixture();

var profile = fixture.Create<ValidatedProfile>();

Assert.False(string.IsNullOrEmpty(profile.Name));
Assert.InRange(profile.Code.Length, 1, ValidatedProfile.CodeMaxLength);

How it works

Supported attributes include [Required], [StringLength], [Range], and others handled by AutoFixture's data annotation relays.

Generated values stay within declared constraints so tests focus on behavior, not invalid input.

Next steps

API