SuperAPI AI 工具集

dotnet-testing-autofixture-customization

测试与质量

查看项目

更新于 10-15 23:45

技能介绍

用途:本技能針對 .NET 測試中 AutoFixture 的進階自訂化,協助在需要控制測試資料生成邏輯、處理特殊型別或自訂建構器時使用。

主要能力:DataAnnotations 自動整合(如 [StringLength]、[Range]);以 .With() 控制屬性範圍,動態值需傳入 lambda,避免固定值只計算一次;自訂 ISpecimenBuilder,涵蓋 RandomRangedDateTimeBuilder、改進版數值範圍建構器與泛型化 NumericRangeBuilder<TValue>(支援 int/long/decimal/double/float);以 Insert(0) 確保優先順序,避免被內建 RangeAttributeRelay、NumericSequenceGenerator 攔截(int 需 Insert(0),DateTime 用 Add() 即可);提供 AddRandomRange 流暢式擴充方法。

安裝套件:<PackageReference Include="AutoFixture" Version="4.18.1" /> 與 <PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />。

用法要點:善用 DataAnnotations、使用 Random.Shared(執行緒安全、效能較佳)、自訂數值建構器務必 Insert(0)、泛型化設計與擴充方法。範例與程式碼範本見 references/specimen-builder-examples.md 與 templates 資料夾。

限制與關聯:屬進階主題,需先掌握 autofixture-basics;後續可接 autodata-xunit-integration 與 autofixture-nsubstitute-integration。具體收費、平台與效果等資訊暫未提供。