Dotnet Testing Test Data Builder Pattern logo

Dotnet Testing Test Data Builder Pattern

Community
kevintsengtw
dotnet-testing-test-data-builder-pattern

Test Data Builder Pattern 完整實作指南。當需要使用建構者模式建立可維護的測試資料或簡化複雜物件的測試準備時使用。涵蓋流暢介面、語意化方法、預設值設計與 Builder 組合模式。 Keywords: test data builder, builder pattern test, 測試資料建構器, object mother, fluent interface, 流暢介面, UserBuilder, ProductBuilder, .With(), .Build(), AUser(), 測試資料準備, 複雜物件建立, 語意化測試

Overview

Publisherkevintsengtw
Repositorydotnet-testing-agent-skills
Skill namedotnet-testing-test-data-builder-pattern
Stars
30
Forks
4
Bundled files
5
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.

  • 5 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 Test Data Builder Pattern 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-test-data-builder-pattern
  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-test-data-builder-pattern .claude/skills/dotnet-testing-test-data-builder-pattern
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Dotnet Testing Test Data Builder Pattern 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 Test Data Builder Pattern 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 Test Data Builder Pattern 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.

Test Data Builder Pattern 測試資料建構器模式

核心概念

什麼是 Test Data Builder Pattern?

Test Data Builder Pattern 是 Object Mother Pattern 的改良版,主要解決以下問題:

  1. 固定測試資料的問題:Object Mother 提供固定的測試物件,難以針對特定測試情境調整
  2. 測試意圖不明確:直接建立物件時,測試的關注點容易被大量屬性設定所掩蓋
  3. 重複程式碼:相似的物件建立邏輯在多個測試中重複出現

為何需要 Builder Pattern?

csharp
// ❌ 問題:過多參數設定,測試意圖不明確
var user = new User
{
    Name = "John Doe", Email = "john@example.com", Age = 30,
    Roles = new[] { "User" }, Settings = new UserSettings { Theme = "Dark", Language = "zh-TW" },
    IsActive = true, CreatedAt = DateTime.Now, ModifiedAt = DateTime.Now
};

// ✅ 改善:意圖明確,只設定測試關注的屬性
var user = UserBuilder.AUser().WithName("John Doe").WithValidEmail().Build();

實作指南

基本 Builder 結構

一個標準的 Test Data Builder 應包含:

  1. 預設值:為所有必要屬性提供合理的預設值
  2. 流暢介面:使用 With* 方法鏈來設定屬性
  3. 語意化方法:提供有意義的預設建立者(如 AnAdminUser()、ARegularUser())
  4. Build 方法:最終建立並回傳目標物件

涵蓋完整 UserBuilder 實作(預設值、With* 流暢方法、語意化靜態工廠方法、Build 方法)、單一測試情境與配合 Theory 使用的範例。

完整 Builder 實作與測試使用範例請參考 references/builder-implementation.md


最佳實踐

涵蓋五項實踐:提供合理的預設值、使用語意化命名(UserScenarios)、Builder 之間的組合(OrderBuilder 組合 UserBuilder + ProductBuilder)、避免過度複雜化、統一管理測試資料(TestData 靜態類別)。

完整最佳實踐與進階模式請參考 references/best-practices-and-patterns.md


與其他模式的比較

Test Data Builder vs. Object Mother

特性Test Data BuilderObject Mother
彈性高度彈性,可針對測試調整固定的測試資料
可讀性流暢介面,意圖明確需要查看方法實作
維護性集中管理,易於修改變更影響所有測試
使用場景單元測試、情境測試簡單的整合測試

Test Data Builder vs. AutoFixture

特性Test Data BuilderAutoFixture
控制度完全控制物件建立自動產生,控制度較低
設定複雜度需手動建立 Builder幾乎零設定
測試意圖非常明確需額外說明
適用時機需要精確控制的測試大量資料產生、匿名測試

建議:Test Data Builder 和 AutoFixture 可以相輔相成。簡單情境使用 AutoFixture,複雜情境或需明確意圖時使用 Builder Pattern。

實戰範例

請參考 templates/ 目錄下的完整實作範例:

  • user-builder-example.cs - 基本 User Builder 實作
  • advanced-builder-scenarios.cs - 進階 Builder 組合與使用情境
  • builder-with-theory.cs - Builder 配合 xUnit Theory 的實務範例

輸出格式

  • 產生 Builder 類別檔案(如 UserBuilder.cs、ProductBuilder.cs),放置於測試專案的 Builders/ 目錄
  • 每個 Builder 包含預設值、With* 流暢方法、語意化靜態工廠方法與 Build() 方法
  • 若需要跨測試共用,產生 TestData.cs 靜態類別集中管理常用測試資料
  • 產生對應的測試類別檔案(*Tests.cs),示範 Builder 的使用方式

參考資源

原始文章

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

延伸閱讀

相關技能

  • autofixture-basics - 使用 AutoFixture 自動產生測試資料
  • xunit-project-setup - xUnit 測試專案的基礎設定
  • test-naming-conventions - 測試命名規範

總結

Test Data Builder Pattern 是撰寫可維護測試的重要技巧:

  • 使用時機:測試物件有多個屬性、需重複使用測試資料、希望表達清晰意圖
  • 核心優勢:提升可讀性、降低維護成本、增強表達力
  • 注意事項:保持 Builder 簡單、提供合理預設值、使用語意化方法名稱

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 Test Data Builder Pattern AI skill do?

Test Data Builder Pattern 完整實作指南。當需要使用建構者模式建立可維護的測試資料或簡化複雜物件的測試準備時使用。涵蓋流暢介面、語意化方法、預設值設計與 Builder 組合模式。 Keywords: test data builder, builder pattern test, 測試資料建構器, object mother, fluent interface, 流暢介面, UserBuilder, ProductBuilder, .With(), .Build(), AUser(), 測試資料準備, 複雜物件建立, 語意化測試

Why use Dotnet Testing Test Data Builder Pattern on TypingMind?

Because you install it once and use it with any model. Dotnet Testing Test Data Builder Pattern 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 Test Data Builder Pattern 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-test-data-builder-pattern. 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 Test Data Builder Pattern?

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 Test Data Builder Pattern?

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

Is the Dotnet Testing Test Data Builder Pattern 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 👇