Featured image of post Generate Test Data with the Bogus Library for C#

Generate Test Data with the Bogus Library for C#

Code Examples in C#

Introduction

If you’ve ever tried writing unit tests, you know how painful it is to generate realistic but fake data manually. Enter Bogus—a powerful library for generating random, realistic, and structured test data in .NET.

Bogus is perfect for:

  • Unit tests – Avoiding manually creating test objects.
  • Database seeding – Populating a dev database with realistic test data.
  • Mock API responses – Simulating API endpoints with randomized responses.

Official GitHub Repo: Bogus by bchavez


Why Use Bogus?

FeatureDescription
Realistic DataGenerates names, emails, addresses, even fake products!
Custom RulesDefine your own data rules easily.
LocalizationSupports multiple languages (e.g., English, French, German).
PerformanceOptimized for speed, even with large datasets.
IntegrationWorks with xUnit, NUnit, MSTest, and Entity Framework.

Getting Started with Bogus

1. Installing Bogus

Add Bogus to your .NET project via NuGet:

1
dotnet add package Bogus

Or via Package Manager Console:

1
Install-Package Bogus

Generating Fake Data

2. Generating a Fake Name

1
2
3
4
5
using Bogus;

var faker = new Faker();
string name = faker.Name.FullName();
Console.WriteLine(name);  // Outputs a random full name

3. Generating Fake Emails and Addresses

1
2
3
var faker = new Faker();
Console.WriteLine(faker.Internet.Email()); // Random email
Console.WriteLine(faker.Address.City());   // Random city name

4. Creating a Custom Fake Object

1
2
3
4
5
6
7
8
var personFaker = new Faker<Person>()
    .RuleFor(p => p.FirstName, f => f.Name.FirstName())
    .RuleFor(p => p.LastName, f => f.Name.LastName())
    .RuleFor(p => p.Email, f => f.Internet.Email())
    .RuleFor(p => p.Birthday, f => f.Date.Past(30));

var fakePerson = personFaker.Generate();
Console.WriteLine($"{fakePerson.FirstName} {fakePerson.LastName}, Email: {fakePerson.Email}");

Generating Fake Data for Unit Tests

5. Using Bogus with xUnit

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class UserServiceTests
{
    [Fact]
    public void CreateUser_ShouldReturnValidUser()
    {
        var userFaker = new Faker<User>()
            .RuleFor(u => u.Id, f => f.Random.Guid())
            .RuleFor(u => u.Name, f => f.Name.FullName())
            .RuleFor(u => u.Email, f => f.Internet.Email());

        var fakeUser = userFaker.Generate();

        Assert.NotNull(fakeUser);
        Assert.NotNull(fakeUser.Email);
    }
}

6. Seeding a Fake Database

1
2
3
4
5
6
7
8
var users = new Faker<User>()
    .RuleFor(u => u.Id, f => f.Random.Guid())
    .RuleFor(u => u.Name, f => f.Name.FullName())
    .Generate(100); // Generates 100 fake users

// Insert into database
dbContext.Users.AddRange(users);
dbContext.SaveChanges();

Comparing Bogus with Alternatives

LibraryFeaturesEase of UseBest For
BogusHighVery EasyGeneral fake data generation
AutoFixtureHighMediumAuto-generating test objects
Faker.NetMediumMediumSimpler but less powerful
MoqLowEasyMocking dependencies, not generating data

Alternative Approaches: Pros & Cons

ApproachProsCons
Using Static DataSimple & predictableHard to maintain
Hand-Coded Test DataControl over test casesRepetitive & time-consuming
Using BogusFast & realisticRequires dependency
AutoFixtureFully automated object creationLess control over data

  • Moq + Bogus: Use Moq for mocking dependencies and Bogus for data generation.
  • Bogus + Entity Framework: Use Bogus for database seeding in EF Core.
  • Bogus + xUnit/NUnit: Use Bogus to generate test objects in unit tests.

Key Ideas

  • Bogus is the best library for generating fake data in C#.
  • It supports realistic names, emails, addresses, and even custom objects.
  • It’s perfect for unit testing, database seeding, and API mock responses.
  • Alternatives like AutoFixture and Moq serve different purposes.
  • Pair Bogus with Moq for ultimate testing flexibility.

References

  1. Bogus GitHub Repository
  2. Bogus Documentation
  3. xUnit Official Docs
  4. Moq GitHub Repository
  5. AutoFixture GitHub Repository
  6. Using Bogus for Database Seeding