---
name: dotnet-testing-autodata-xunit-integration
description: |
AutoFixture 與 xUnit 整合完整指南。
涵蓋 AutoData、InlineAutoData、自訂 Customization 與測試資料屬性。
簡化參數化測試資料準備,提升測試可讀性與維護性。
triggers:
# 核心關鍵字
- AutoData
- InlineAutoData
- AutoFixture xUnit
- xUnit AutoFixture
# 技術術語
- [AutoData]
- [InlineAutoData]
- AutoDataAttribute
- ICustomization
- DataAttribute
# 類別名稱
- ValidUserCustomization
- CustomAutoDataAttribute
- DomainCustomization
# 使用情境
- 參數化測試
- 自訂測試資料
- Theory AutoData
- MemberAutoData
- 測試資料屬性
- fixture.Customize
license: MIT
metadata:
author: Kevin Tseng
version: "1.0.0"
tags: "autofixture, xunit, autodata, theory, parameterized-tests, customization"
---
# AutoData 屬性家族:xUnit 與 AutoFixture 的整合應用
## 觸發關鍵字
- AutoData
- InlineAutoData
- MemberAutoData
- CompositeAutoData
- xUnit AutoFixture 整合
- 參數化測試資料
- 測試參數注入
- CollectionSizeAttribute
- 外部測試資料
- CSV 測試資料
- JSON 測試資料
## 概述
AutoData 屬性家族是 `AutoFixture.Xunit2` 套件提供的功能,將 AutoFixture 的資料產生能力與 xUnit 的參數化測試整合,讓測試參數自動注入,大幅減少測試準備程式碼。
### 核心特色
1. **AutoData**:自動產生所有測試參數
2. **InlineAutoData**:混合固定值與自動產生
3. **MemberAutoData**:結合外部資料來源
4. **CompositeAutoData**:多重資料來源整合
5. **CollectionSizeAttribute**:控制集合產生數量
## 安裝套件
```xml
```
```bash
dotnet add package AutoFixture.Xunit2
```
## AutoData:完全自動產生參數
`AutoData` 是最基礎的屬性,自動為測試方法的所有參數產生測試資料。
### 基本使用
```csharp
using AutoFixture.Xunit2;
public class Person
{
public Guid Id { get; set; }
[StringLength(10)]
public string Name { get; set; } = string.Empty;
[Range(18, 80)]
public int Age { get; set; }
public string Email { get; set; } = string.Empty;
public DateTime CreateTime { get; set; }
}
[Theory]
[AutoData]
public void AutoData_應能自動產生所有參數(Person person, string message, int count)
{
// Arrange & Act - 參數已由 AutoData 自動產生
// Assert
person.Should().NotBeNull();
person.Id.Should().NotBe(Guid.Empty);
person.Name.Should().HaveLength(10); // 遵循 StringLength 限制
person.Age.Should().BeInRange(18, 80); // 遵循 Range 限制
message.Should().NotBeNullOrEmpty();
count.Should().NotBe(0);
}
```
### 透過 DataAnnotation 約束參數
```csharp
[Theory]
[AutoData]
public void AutoData_透過DataAnnotation約束參數(
[StringLength(5, MinimumLength = 3)] string shortName,
[Range(1, 100)] int percentage,
Person person)
{
// Assert
shortName.Length.Should().BeInRange(3, 5);
percentage.Should().BeInRange(1, 100);
person.Should().NotBeNull();
}
```
## InlineAutoData:混合固定值與自動產生
`InlineAutoData` 結合了 `InlineData` 的固定值特性與 `AutoData` 的自動產生功能。
### 基本語法
```csharp
[Theory]
[InlineAutoData("VIP客戶", 1000)]
[InlineAutoData("一般客戶", 500)]
[InlineAutoData("新客戶", 100)]
public void InlineAutoData_混合固定值與自動產生(
string customerType, // 對應第 1 個固定值
decimal creditLimit, // 對應第 2 個固定值
Person person) // 由 AutoFixture 產生
{
// Arrange
var customer = new Customer
{
Person = person,
Type = customerType,
CreditLimit = creditLimit
};
// Assert
customer.Type.Should().Be(customerType);
customer.CreditLimit.Should().BeOneOf(1000, 500, 100);
customer.Person.Should().NotBeNull();
}
```
### 參數順序一致性
固定值的順序必須與方法參數順序一致:
```csharp
[Theory]
[InlineAutoData("產品A", 100)] // 依序對應 name, price
[InlineAutoData("產品B", 200)]
public void InlineAutoData_參數順序一致性(
string name, // 對應第 1 個固定值
decimal price, // 對應第 2 個固定值
string description, // 由 AutoFixture 產生
Product product) // 由 AutoFixture 產生
{
// Assert
name.Should().BeOneOf("產品A", "產品B");
price.Should().BeOneOf(100, 200);
description.Should().NotBeNullOrEmpty();
product.Should().NotBeNull();
}
```
### ⚠️ 重要限制:只能使用編譯時常數
```csharp
// ✅ 正確:使用常數
[InlineAutoData("VIP", 100000)]
[InlineAutoData("Premium", 50000)]
// ❌ 錯誤:不能使用變數
private const decimal VipCreditLimit = 100000m;
[InlineAutoData("VIP", VipCreditLimit)] // 編譯錯誤
// ❌ 錯誤:不能使用運算式
[InlineAutoData("VIP", 100 * 1000)] // 編譯錯誤
```
需要使用複雜資料時,應使用 `MemberAutoData`。
## MemberAutoData:結合外部資料來源
`MemberAutoData` 允許從類別的方法、屬性或欄位中獲取測試資料。
### 使用靜態方法
```csharp
public class MemberAutoDataTests
{
public static IEnumerable