Specialized skill for generating realistic fake data using Bogus. Use it when you need believable names, addresses, phone numbers, emails, company info, etc. for testing. Covers Faker classes, multilingual support, custom rules, bulk generation, and more. Keywords: bogus, faker, fake data, realistic data, fake name, fake address, fake email, Faker<T>, RuleFor, Generate, faker.Name, faker.Address, faker.Internet, generate fake data, seed data
dotnet add package Bogus
| 套件名稱 | 用途 | NuGet 連結 |
| -------- | ---------------- | -------------------------------------------------- |
| Bogus | 假資料產生函式庫 | nuget.org |
GitHub 儲存庫:bchavez/Bogus
Bogus 的核心是 Faker<T> 類別,使用 RuleFor 方法定義屬性的產生規則:
using Bogus;
public class Product
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public string Description { get; set; } = string.Empty;
public DateTime CreatedDate { get; set; }
}
// 建立產品資料的 Faker
var productFaker = new Faker<Product>()
.RuleFor(p => p.Id, f => f.IndexFaker)
.RuleFor(p => p.Name, f => f.Commerce.ProductName())
.RuleFor(p => p.Price, f => f.Random.Decimal(10, 1000))
.RuleFor(p => p.Description, f => f.Lorem.Sentence())
.RuleFor(p => p.CreatedDate, f => f.Date.Past());
// 產生單筆資料
var product = productFaker.Generate();
// 產生多筆資料
var products = productFaker.Generate(10);
Bogus 提供豐富的內建 DataSet,每個都專注於特定領域的資料產生:
var faker = new Faker();
var fullName = faker.Person.FullName; // 完整姓名
var firstName = faker.Person.FirstName; // 名字
var lastName = faker.Person.LastName; // 姓氏
var email = faker.Person.Email; // 電子郵件
var gender = faker.Person.Gender; // 性別
var dateOfBirth = faker.Person.DateOfBirth; // 生日
var fullAddress = faker.Address.FullAddress(); // 完整地址
var streetAddress = faker.Address.StreetAddress(); // 街道地址
var city = faker.Address.City(); // 城市
var state = faker.Address.State(); // 州/省
var zipCode = faker.Address.ZipCode(); // 郵遞區號
var country = faker.Address.Country(); // 國家
var latitude = faker.Address.Latitude(); // 緯度
var longitude = faker.Address.Longitude(); // 經度
var companyName = faker.Company.CompanyName(); // 公司名稱
var catchPhrase = faker.Company.CatchPhrase(); // 標語
var department = faker.Commerce.Department(); // 部門
var productName = faker.Commerce.ProductName(); // 產品名稱
var price = faker.Commerce.Price(1, 1000, 2); // 價格(字串格式)
var ean13 = faker.Commerce.Ean13(); // EAN-13 條碼
var url = faker.Internet.Url(); // URL
var domainName = faker.Internet.DomainName(); // 網域名稱
var ipAddress = faker.Internet.Ip(); // IPv4 地址
var ipv6 = faker.Internet.Ipv6(); // IPv6 地址
var userName = faker.Internet.UserName(); // 使用者名稱
var password = faker.Internet.Password(); // 密碼
var email = faker.Internet.Email(); // 電子郵件
var creditCardNumber = faker.Finance.CreditCardNumber(); // 信用卡號
var creditCardCvv = faker.Finance.CreditCardCvv(); // CVV
var account = faker.Finance.Account(); // 帳戶號碼
var amount = faker.Finance.Amount(100, 10000, 2); // 金額
var currency = faker.Finance.Currency(); // 貨幣
var iban = faker.Finance.Iban(); // IBAN
var bic = faker.Finance.Bic(); // BIC/SWIFT
var pastDate = faker.Date.Past(); // 過去日期
var futureDate = faker.Date.Future(); // 未來日期
var recentDate = faker.Date.Recent(); // 最近日期
var soonDate = faker.Date.Soon(); // 即將到來的日期
var between = faker.Date.Between(start, end); // 範圍內日期
var weekday = faker.Date.Weekday(); // 星期幾
var randomInt = faker.Random.Int(1, 100); // 整數
var randomDecimal = faker.Random.Decimal(0, 1000); // 小數
var randomBool = faker.Random.Bool(); // 布林
var randomGuid = faker.Random.Guid(); // GUID
var randomEnum = faker.Random.Enum<DayOfWeek>(); // 隨機列舉
var randomElement = faker.Random.ArrayElement(array); // 陣列隨機元素
var shuffled = faker.Random.Shuffle(collection); // 洗牌
var word = faker.Lorem.Word(); // 單字
var words = faker.Lorem.Words(5); // 多個單字
var sentence = faker.Lorem.Sentence(); // 句子
var paragraph = faker.Lorem.Paragraph(); // 段落
var text = faker.Lorem.Text(); // 文字區塊
Bogus 的一大特色是支援多種語言和文化,讓產生的資料更符合當地習慣:
// 繁體中文
var chineseFaker = new Faker<Person>("zh_TW")
.RuleFor(p => p.Name, f => f.Person.FullName)
.RuleFor(p => p.Address, f => f.Address.FullAddress());
// 日文
var japaneseFaker = new Faker<Person>("ja")
.RuleFor(p => p.Name, f => f.Person.FullName)
.RuleFor(p => p.Phone, f => f.Phone.PhoneNumber());
// 法文
var frenchFaker = new Faker<Person>("fr")
.RuleFor(p => p.Name, f => f.Person.FullName)
.RuleFor(p => p.Company, f => f.Company.CompanyName());
| 語言 | 代碼 | 語言 | 代碼 |
| ------------ | ------- | -------- | ------- |
| 英文(美國) | en_US | 簡體中文 | zh_CN |
| 繁體中文 | zh_TW | 日文 | ja |
| 韓文 | ko | 法文 | fr |
| 德文 | de | 西班牙文 | es |
| 俄文 | ru | 葡萄牙文 | pt_BR |
涵蓋 Seed 可重現性控制、條件式產生與機率控制(OrNull、PickRandomWeighted)、關聯資料與巢狀物件、複雜業務邏輯約束,以及自訂 DataSet 擴充(如台灣在地資料產生器)。
完整內容請參閱 references/advanced-features.md
| 項目 | AutoFixture | Bogus | | ------------ | ------------------------- | ------------------------------- | | 核心理念 | 匿名測試 (Anonymous Test) | 真實模擬 (Realistic Simulation) | | 資料品質 | 隨機填充,專注測試邏輯 | 有意義資料,模擬真實情境 | | 學習成本 | 自動推斷,零配置 | 明確定義,需要學習 DataSet | | 可讀性 | 抽象化,減少資料噪音 | 具體化,資料有意義 |
| 場景 | 建議工具 | 原因 | | -------------- | ----------- | ------------------------------ | | 單元測試 | AutoFixture | 專注於邏輯驗證,不關心資料內容 | | 整合測試 | Bogus | 需要真實感的資料進行端到端測試 | | UI 原型 | Bogus | 展示用的擬真資料 | | 效能測試 | Bogus | 大量真實格式的資料 | | 資料庫種子 | Bogus | 初始化開發/測試環境 | | 複雜相依性 | AutoFixture | 自動處理循環參考和巢狀物件 |
// AutoFixture:簡單直接,自動推斷
var fixture = new Fixture();
var user = fixture.Create<User>(); // 一行搞定,但資料無意義
// Bogus:需要設定,但資料有意義
var userFaker = new Faker<User>()
.RuleFor(u => u.Name, f => f.Person.FullName) // 真實的姓名格式
.RuleFor(u => u.Email, f => f.Internet.Email()); // 真實的郵件格式
var user = userFaker.Generate();
public class OptimizedDataGenerator
{
// 預編譯 Faker 以提升效能(靜態欄位,只初始化一次)
private static readonly Faker<User> _userFaker = new Faker<User>()
.RuleFor(u => u.Id, f => f.Random.Guid())
.RuleFor(u => u.Name, f => f.Person.FullName)
.RuleFor(u => u.Email, f => f.Internet.Email());
public static List<User> GenerateUsers(int count)
=> _userFaker.Generate(count);
}
// 批次產生以減少記憶體分配
public static IEnumerable<User> GenerateUsersBatch(int totalCount, int batchSize = 1000)
{
var generated = 0;
while (generated < totalCount)
{
var currentBatchSize = Math.Min(batchSize, totalCount - generated);
var batch = _userFaker.Generate(currentBatchSize);
foreach (var user in batch)
{
yield return user;
}
generated += currentBatchSize;
}
}
// 使用 Lazy 延遲初始化複雜的 Faker
private static readonly Lazy<Faker<ComplexEntity>> _complexFaker =
new(() => new Faker<ComplexEntity>()
.RuleFor(e => e.Id, f => f.Random.Guid())
.RuleFor(e => e.Data, f => GenerateComplexData(f)));
public static ComplexEntity Generate() => _complexFaker.Value.Generate();
[Fact]
public void EmailService_SendWelcomeEmail_ShouldFormatCorrectly()
{
// Arrange - 需要真實的使用者資料來測試郵件格式
var userFaker = new Faker<User>()
.RuleFor(u => u.Name, f => f.Person.FullName)
.RuleFor(u => u.Email, f => f.Internet.Email());
var user = userFaker.Generate();
var emailService = new EmailService();
// Act
var emailContent = emailService.GenerateWelcomeEmail(user);
// Assert
emailContent.Should().Contain(user.Name);
emailContent.Should().Contain(user.Email);
}
public static class DatabaseSeeder
{
public static void SeedDatabase(AppDbContext context)
{
// 設定 seed 確保可重現
Randomizer.Seed = new Random(42);
var customerFaker = new Faker<Customer>("zh_TW")
.RuleFor(c => c.Name, f => f.Person.FullName)
.RuleFor(c => c.Email, f => f.Internet.Email())
.RuleFor(c => c.Phone, f => f.Phone.PhoneNumber())
.RuleFor(c => c.Address, f => f.Address.FullAddress());
var customers = customerFaker.Generate(100);
context.Customers.AddRange(customers);
context.SaveChanges();
}
}
{EntityName}Faker 格式命名TestDataGenerators 或 Fakers 資料夾MyProject.Tests/
├── Fakers/
│ ├── CustomerFaker.cs
│ ├── OrderFaker.cs
│ └── TaiwanDataSetExtensions.cs
├── Services/
│ └── CustomerServiceTests.cs
└── ...
| 技能名稱 | 關聯說明 |
| ------------------------------- | -------------------------------------------- |
| autofixture-basics | AutoFixture 基礎使用,適合單元測試的匿名資料 |
| autofixture-bogus-integration | AutoFixture 與 Bogus 混合使用策略 |
| test-data-builder-pattern | 手動 Builder Pattern,適合簡單場景 |
本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章:
npx skills add kevintsengtw/dotnet-testing-bogus-假数据下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer