dotnet-testing-autofixture-basics — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited dotnet-testing-autofixture-basics (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />或透過命令列安裝:
dotnet add package AutoFixture
dotnet add package AutoFixture.Xunit2| API | 用途 | 預設行為 |
|---|---|---|
fixture.Create<T>() | 產生單一物件 | 自動填充所有屬性 |
fixture.CreateMany<T>() | 產生集合 | 預設 3 個元素 |
fixture.Build<T>() | 精確控制屬性 | 搭配 With/Without |
OmitAutoProperties() | 只設定必要屬性 | 其餘保持預設值 |
Fixture 是核心類別,提供自動產生測試資料的能力:
int(隨機正整數)、string(GUID 格式)、decimal、bool、DateTime、GuidCreateMany<T>() 預設產生 3 個元素,可指定數量如 CreateMany<T>(10)完整程式碼範例請參考 references/basic-usage-examples.md
當需要對特定屬性進行控制時,使用 Build<T>() 模式:
| 方法 | 用途 | 範例 |
|---|---|---|
.With(prop, value) | 指定固定值 | .With(x => x.Name, "測試客戶") |
.With(prop, factory) | 使用工廠方法產生值 | .With(x => x.Price, () => random.Next()) |
.Without(prop) | 排除屬性,保持預設值 | .Without(x => x.InternalId) |
.OmitAutoProperties() | 不自動設定任何屬性 | 只設定關心的屬性 |
當物件包含循環參考時(如 Category 的 Parent 屬性指向自己),AutoFixture 預設會拋出 ObjectCreationException。
解決方案: 使用 OmitOnRecursionBehavior,移除預設的 ThrowingRecursionBehavior 並加入忽略循環參考的行為。
建議: 建立 AutoFixtureTestBase 基底類別統一處理循環參考,避免在每個測試中重複設定。
完整程式碼範例請參考 references/build-and-customization.md
在測試類別建構函式中使用 Customize<T>() 設定共用的資料產生規則,所有測試方法共享同一個 Fixture 設定。
搭配 [Theory] + [InlineData],使用 Build<T>().With() 針對不同輸入情境設定關鍵屬性值,驗證各情境的預期行為。
測試應該關注「行為」而不是「資料」:
fixture.Create<T>() 產生任意有效資料,驗證操作結果Build<T>().With() 明確設定影響測試結果的關鍵值完整程式碼範例請參考 references/xunit-integration-examples.md
| 層面 | Test Data Builder | AutoFixture |
|---|---|---|
| 程式碼行數 | 40+ 行 Builder + 測試 | 5 行測試 |
| 維護成本 | 物件改變需更新 Builder | 自動適應變化 |
| 開發時間 | 先寫 Builder 再寫測試 | 直接寫測試 |
| 大量資料 | 需要迴圈 | CreateMany(100) |
| 可讀性 | 業務語意明確 | 需理解 AutoFixture |
完整比較程式碼與混合策略請參考 references/xunit-integration-examples.md
結合 Test Data Builder 與 AutoFixture 的優點:
TestDataFactory 封裝常用的資料產生模式CreateMany<T>(count)完整比較程式碼與混合策略請參考 references/xunit-integration-examples.md
AutoFixture 在實務中常用於以下場景:
Build<T>() 產生符合驗證規則的資料CreateMany() 產生批次資料完整程式碼範例請參閱 references/practical-scenarios.md
預設產生 3 個元素,可透過參數指定數量:fixture.CreateMany<T>(10)。若要全域調整,使用 fixture.RepeatCount = 5。
使用 Build<T>().With() 指定格式,或搭配 Customize<T>() 設定全域規則。若需要真實感資料,考慮整合 Bogus。
Build<T>().With() 設定關鍵屬性CreateMany() 數量請參考 templates 資料夾中的範例檔案:
dotnet-testing-autofixture-customization - AutoFixture 進階自訂dotnet-testing-autofixture-bogus-integration - AutoFixture + Bogus 整合dotnet-testing-autofixture-nsubstitute-integration - AutoFixture + NSubstitute 整合dotnet-testing-autodata-xunit-integration - AutoData 與 xUnit 整合dotnet-testing-test-data-builder-pattern - Test Data Builder Pattern~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.