Dotnet Testing Autofixture Customization logo

Dotnet Testing Autofixture Customization

Community
kevintsengtw
dotnet-testing-autofixture-customization

AutoFixture 進階自訂化技術完整指南。當需要自訂 AutoFixture 建構器或處理特殊型別的測試資料產生規則時使用。涵蓋 DataAnnotations 自動整合、ISpecimenBuilder 實作、優先順序管理。包含 DateTime/數值範圍建構器、泛型化設計與流暢式擴充方法。 Keywords: autofixture customization, autofixture customize, autofixture 自訂, specimen builder, ISpecimenBuilder, RandomDateTimeSequenceGenerator, NumericRangeBuilder, DataAnnotations autofixture, fixture.Customizations, Insert(0), 自訂建構器, NoSpecimen, 泛型化建構器

Overview

Publisherkevintsengtw
Repositorydotnet-testing-agent-skills
Skill namedotnet-testing-autofixture-customization
Stars
30
Forks
4
Bundled files
4
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 4 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by kevintsengtw on GitHub. Read the source before you install it.

Installation

Install the Dotnet Testing Autofixture Customization AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-autofixture-customization
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/kevintsengtw/dotnet-testing-agent-skills.git /tmp/dotnet-testing-agent-skills
mkdir -p .claude/skills
cp -r /tmp/dotnet-testing-agent-skills/skills/dotnet-testing-autofixture-customization .claude/skills/dotnet-testing-autofixture-customization
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Dotnet Testing Autofixture Customization in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Dotnet Testing Autofixture Customization on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Dotnet Testing Autofixture Customization is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

AutoFixture 進階:自訂化測試資料生成策略

概述

本技能涵蓋 AutoFixture 的進階自訂化功能,讓您能根據業務需求精確控制測試資料的生成邏輯。

核心技術

  1. DataAnnotations 整合:AutoFixture 自動識別 [StringLength]、[Range] 等驗證屬性
  2. 屬性範圍控制:使用 .With() 配合 Random.Shared 動態產生隨機值
  3. 自訂 ISpecimenBuilder:實作精確控制特定屬性的建構器
  4. 優先順序管理:理解 Insert(0) vs Add() 的差異
  5. 泛型化設計:建立支援多種數值型別的可重用建構器

安裝套件

xml
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />

DataAnnotations 自動整合

AutoFixture 能自動識別 System.ComponentModel.DataAnnotations 的驗證屬性:

csharp
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() 控制屬性範圍

固定值 vs 動態值

csharp
// ❌ 固定值:只執行一次,所有物件相同值
.With(x => x.Age, Random.Shared.Next(30, 50))

// ✅ 動態值:每個物件都重新計算
.With(x => x.Age, () => Random.Shared.Next(30, 50))

Random.Shared 的優點

特性new Random()Random.Shared
實例化方式每次建立新實例全域共用單一實例
執行緒安全不是是
效能多次建立有負擔,可能重複值效能更佳,避免重複值

自訂 ISpecimenBuilder

涵蓋 RandomRangedDateTimeBuilder(精確控制特定 DateTime 屬性)、ImprovedRandomRangedNumericSequenceBuilder(改進版數值範圍建構器)、泛型化 NumericRangeBuilder<TValue>(支援 int/long/decimal/double/float 等多種型別),以及流暢介面擴充方法 AddRandomRange。每個建構器皆附完整實作與使用範例。

完整 ISpecimenBuilder 實作範例請參考 references/specimen-builder-examples.md

優先順序管理:Insert(0) vs Add()

AutoFixture 內建的 RangeAttributeRelay、NumericSequenceGenerator 可能比自訂建構器有更高優先順序:

csharp
// ❌ 可能失效:被內建建構器攔截
fixture.Customizations.Add(new MyNumericBuilder(30, 50, "Age"));

// ✅ 正確:確保最高優先順序
fixture.Customizations.Insert(0, new MyNumericBuilder(30, 50, "Age"));

int vs DateTime 處理差異

型別內建建構器優先順序影響
intRangeAttributeRelay、NumericSequenceGenerator會被攔截,需用 Insert(0)
DateTime無特定建構器不會被攔截,Add() 即可

最佳實踐

應該做

  1. 善用 DataAnnotations — 充分利用現有模型驗證規則,AutoFixture 自動產生符合限制的資料
  2. 使用 Random.Shared — 避免重複值問題,執行緒安全、效能更好
  3. Insert(0) 確保優先順序 — 自訂數值建構器務必用 Insert(0)
  4. 泛型化設計 — 建立可重用的泛型建構器,使用擴充方法提供流暢介面

應該避免

  1. 忽略建構器優先順序 — 不要假設 Add() 一定生效
  2. 過度複雜的邏輯 — 建構器保持單一職責
  3. 使用 new Random() — 可能產生重複值,非執行緒安全

程式碼範本

請參考 templates 資料夾中的範例檔案:

與其他技能的關係

  • autofixture-basics:本技能的前置知識,需先掌握基礎用法
  • autodata-xunit-integration:下一步學習目標,將自訂化與 xUnit 整合
  • autofixture-nsubstitute-integration:進階整合,結合 Mock 與自訂資料生成

輸出格式

  • 產生自訂 ISpecimenBuilder 實作類別
  • 產生 ICustomization 組合類別
  • 提供 fixture.Customizations.Insert(0, ...) 設定範例
  • 包含 DataAnnotations 整合與泛型化建構器程式碼

參考資源

原始文章

本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章:

官方文件

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Dotnet Testing Autofixture Customization AI skill do?

AutoFixture 進階自訂化技術完整指南。當需要自訂 AutoFixture 建構器或處理特殊型別的測試資料產生規則時使用。涵蓋 DataAnnotations 自動整合、ISpecimenBuilder 實作、優先順序管理。包含 DateTime/數值範圍建構器、泛型化設計與流暢式擴充方法。 Keywords: autofixture customization, autofixture customize, autofixture 自訂, specimen builder, ISpecimenBuilder, RandomDateTimeSequenceGenerator, NumericRangeBuilder, DataAnnotations autofixture, fixture.Customizations, Insert(0), 自訂建構器, NoSpecimen, 泛型化建構器

Why use Dotnet Testing Autofixture Customization on TypingMind?

Because you install it once and use it with any model. Dotnet Testing Autofixture Customization is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Dotnet Testing Autofixture Customization in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-autofixture-customization. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Dotnet Testing Autofixture Customization?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Dotnet Testing Autofixture Customization?

As many as you like. As long as a model supports skills, you can use Dotnet Testing Autofixture Customization with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Dotnet Testing Autofixture Customization AI skill free?

Yes. It is published on GitHub by kevintsengtw under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇