dotnet-testing-autofixture-customization_skill

This skill guides advanced AutoFixture customization for .NET tests, enabling precise data generation with ISpecimenBuilder, DataAnnotations, and fluent
  • C#

19

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-autofixture-customization

  • SKILL.md14.6 KB

Overview

This skill is a practical guide to advanced AutoFixture customization for .NET tests. It explains how to integrate DataAnnotations, implement ISpecimenBuilder for precise control, and manage builder priority so generated test data matches business constraints. The content includes reusable generic numeric builders, DateTime range generators, and fluent extension methods for easy fixture composition.

How this skill works

The skill inspects property requests made to AutoFixture and provides custom values when a request matches configured predicates (property name, declaring type, or attribute). Custom ISpecimenBuilder implementations return a concrete value for matching requests or NoSpecimen to defer to the next builder. Insert(0) is used to ensure custom builders run before built-in relays that might intercept numeric or ranged values.

When to use it

  • When model validation attributes (StringLength, Range, etc.) must be honored in generated test data.
  • When specific properties need values constrained to a DateTime or numeric range.
  • When built-in AutoFixture generators produce undesired values for certain properties.
  • When creating reusable, type-safe range generators across multiple value types.
  • When you need deterministic but varied test datasets for integration or property tests.

Best practices

  • Leverage DataAnnotations on models and let AutoFixture respect them where possible.
  • Use Random.Shared for thread-safe, non-repeating random value generation.
  • Register numeric or property-specific builders with fixture.Customizations.Insert(0) to guarantee priority.
  • Keep specimen builders single-responsibility and return NoSpecimen for non-target requests.
  • Provide fluent extension methods to compose multiple range rules clearly in tests.

Example use cases

  • Create Persons where Name length and Age match StringLength and Range attributes automatically.
  • Control UpdateTime only, generating dates between two boundaries while leaving other DateTime fields untouched.
  • Enforce Price, Quantity, Rating, Discount ranges on Product objects using a generic NumericRangeBuilder<T>.
  • Replace built-in numeric generators for a domain property (e.g., Age) to avoid interception by RangeAttributeRelay.
  • Add chained AddRandomRange<T, TValue> calls to a Fixture to express complex test data constraints concisely.

FAQ

Built-in AutoFixture relays like RangeAttributeRelay or NumericSequenceGenerator can intercept numeric requests. Insert(0) puts your builder at highest priority so your rule executes first.

When should a builder return NoSpecimen?

Return NoSpecimen for any request your builder does not target (wrong type, property name, or attribute). This delegates creation to the next builder in the chain and avoids side effects.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational