Summary
- AutoFixture 進階自訂化技術完整指南。當需要自訂 AutoFixture 建構器或處理特殊型別的測試資料產生規則時使用。涵蓋 DataAnnotations 自動整合、ISpecimenBuilder 實作、優先順序管理。包含 DateTime/數值範圍建構器、泛型化設計與流暢式擴充方法。
- Make…
kevintsengtw/dotnet-testing-agent-skills
AutoFixture 進階自訂化技術完整指南。當需要自訂 AutoFixture 建構器或處理特殊型別的測試資料產生規則時使用。涵蓋 DataAnnotations 自動整合、ISpecimenBuilder 實作、優? Make sure to use this skill whenever the user mentions AutoFixture customization, ISpecimenBuilder, DataAnnotations with AutoFixture, or custom builders for test data generation, even if they don't explicitly ask for customization guidance.
npx skills add kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-autofixture-customization
Related neighbors and high-traction skills in the same topics — useful to compare before installing.
Browser automation CLI for AI agents. Use when the user needs to interact with websites, includ…
810.4K installsConfigure Azure API Management as an AI Gateway for AI models, MCP tools, and agents. WHEN: sem…
566.3K installsAzure VM/VMSS router. WHEN: create / provision / deploy / spin-up VM, recommend VM size, compar…
510K installsPostgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill …
391.6K installsPrisma ORM CLI commands reference covering init, generate, migrate, db, dev, complete, studio, …
267K installsUse when doing ANY task involving Supabase. Triggers: Supabase products (Database, Auth, Edge F…
263K installsOther skills from kevintsengtw/dotnet-testing-agent-skills · top by installs.
npx skills add kevintsengtw/dotnet-testing-agent-skills
Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.
Alternate registries and mirrors of this skill.
npx skills add smithery/kevintsengtw --skill dotnet-testing-autofixture-customization
main
Files included with this skill beyond the listing page.
SKILL.md
6,608 B
SUMMARY.md
883 B
本技能涵蓋 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](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](./templates) 資料夾中的範例檔案:
本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章:
- 鐵人賽文章:https://ithelp.ithome.com.tw/articles/10375153 - 範例程式碼:https://github.com/kevintsengtw/30DaysinTesting_Samples/tree/main/day11