Comprehensive guide to advanced AutoFixture customization techniques. Use when you need to customize AutoFixture builders or define test-data generation rules for special types. Covers automatic integration with DataAnnotations, implementing ISpecimenBuilder, and priority/ordering management. Includes DateTime and numeric-range builders, generic design patterns, and fluent extension methods. Keywords: autofixture customization, autofixture customize, autofixture custom, specimen builder, ISpecimenBuilder, RandomDateTimeSequenceGenerator, NumericRangeBuilder, DataAnnotations autofixture, fixture.Customizations, Insert(0), custom builder, NoSpecimen, generic builder
本技能涵蓋 AutoFixture 的進階自訂化功能,讓您能根據業務需求精確控制測試資料的生成邏輯。
[StringLength]、[Range] 等驗證屬性.With() 配合 Random.Shared 動態產生隨機值Insert(0) vs Add() 的差異<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
AutoFixture 能自動識別 System.ComponentModel.DataAnnotations 的驗證屬性:
public class Person
{
public Guid Id { get; set; }
[StringLength(10)]
public string Name { get; set; } = string.Empty;
[Range(10, 80)]
public int Age { get; set; }
public DateTime CreateTime { get; set; }
}
[Fact]
public void AutoFixture_應能識別DataAnnotations()
{
var fixture = new Fixture();
var person = fixture.Create<Person>();
person.Name.Length.Should().Be(10); // StringLength(10)
person.Age.Should().BeInRange(10, 80); // Range(10, 80)
}
// ❌ 固定值:只執行一次,所有物件相同值
.With(x => x.Age, Random.Shared.Next(30, 50))
// ✅ 動態值:每個物件都重新計算
.With(x => x.Age, () => Random.Shared.Next(30, 50))
| 特性 | new Random() | Random.Shared |
| ---------- | -------------------------- | -------------------- |
| 實例化方式 | 每次建立新實例 | 全域共用單一實例 |
| 執行緒安全 | 不是 | 是 |
| 效能 | 多次建立有負擔,可能重複值 | 效能更佳,避免重複值 |
涵蓋 RandomRangedDateTimeBuilder(精確控制特定 DateTime 屬性)、ImprovedRandomRangedNumericSequenceBuilder(改進版數值範圍建構器)、泛型化 NumericRangeBuilder<TValue>(支援 int/long/decimal/double/float 等多種型別),以及流暢介面擴充方法 AddRandomRange。每個建構器皆附完整實作與使用範例。
完整 ISpecimenBuilder 實作範例請參考 references/specimen-builder-examples.md
AutoFixture 內建的 RangeAttributeRelay、NumericSequenceGenerator 可能比自訂建構器有更高優先順序:
// ❌ 可能失效:被內建建構器攔截
fixture.Customizations.Add(new MyNumericBuilder(30, 50, "Age"));
// ✅ 正確:確保最高優先順序
fixture.Customizations.Insert(0, new MyNumericBuilder(30, 50, "Age"));
| 型別 | 內建建構器 | 優先順序影響 |
| ---------- | ------------------------------------------------- | -------------------------- |
| int | RangeAttributeRelay、NumericSequenceGenerator | 會被攔截,需用 Insert(0) |
| DateTime | 無特定建構器 | 不會被攔截,Add() 即可 |
Insert(0)Add() 一定生效請參考 templates 資料夾中的範例檔案:
本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章:
npx skills add kevintsengtw/dotnet-testing-autofixture-自定义下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer