Generating Test Data with the Bogus Library in C#

Valentyn Osidach 📚
.Net Programming
Published in
4 min readAug 12, 2024

In software development, the importance of reliable and realistic test data cannot be overstated. Whether you are performing unit tests, stress tests, or simply needing sample data for development, generating accurate and varied test data is crucial. This is where the Bogus library in C# comes into play. Bogus is a powerful and easy-to-use library for generating fake data for .NET applications. In this article, we’ll explore the features of the Bogus library, its installation, and practical examples of its usage.

What is Bogus?

Bogus is a C# library designed for generating realistic fake data. It supports a wide range of data types and scenarios, making it an ideal tool for creating mock data for testing and development purposes. Bogus is highly configurable and can generate data for common scenarios such as names, addresses, dates, phone numbers, and even custom complex objects.

Key Features

  1. Ease of Use: Simple API for generating data.
  2. Extensive Data Types: Supports a wide array of data types and formats.
  3. Localization: Provides localized data in multiple languages.
  4. Customizability: Allows custom rules and scenarios for generating data.
  5. Integration: Easily integrates with unit testing frameworks.

Installing Bogus

To get started with Bogus, you need to install the library via NuGet. You can do this using the NuGet Package Manager in Visual Studio or by running the following command in the .NET CLI:

dotnet add package Bogus

Getting Started with Bogus

Once installed, you can start using Bogus to generate fake data. Below are some basic examples to illustrate its usage.

Basic Usage

Let’s start with a simple example of generating fake names and email addresses:

var faker = new Faker();

for (int i = 0; i < 10; i++)
{
string name = faker.Name.FullName();
string email = faker.Internet.Email();

Console.WriteLine($"Name: {name}, Email: {email}");
}

Generating Complex Objects

Bogus excels at generating complex objects with nested properties. Here’s an example of generating fake data for a User object:

using Bogus;

public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public DateTime DateOfBirth { get; set; }
public Address Address { get; set; }
}

public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
var addressFaker = new Faker<Address>()
.RuleFor(a => a.Street, f => f.Address.StreetAddress())
.RuleFor(a => a.City, f => f.Address.City())
.RuleFor(a => a.State, f => f.Address.State())
.RuleFor(a => a.ZipCode, f => f.Address.ZipCode());

var userFaker = new Faker<User>()
.RuleFor(u => u.Id, f => f.IndexFaker + 1)
.RuleFor(u => u.FirstName, f => f.Name.FirstName())
.RuleFor(u => u.LastName, f => f.Name.LastName())
.RuleFor(u => u.Email, f => f.Internet.Email())
.RuleFor(u => u.DateOfBirth, f => f.Date.Past(30))
.RuleFor(u => u.Address, f => addressFaker.Generate());

var users = userFaker.Generate(5);

foreach (var user in users)
{
Console.WriteLine($"Name: {user.FirstName} {user.LastName}, Email: {user.Email}, Address: {user.Address.Street}, {user.Address.City}, {user.Address.State}, {user.Address.ZipCode}");
}

Custom Data Generation

public class CustomUser
{
public string Username { get; set; }
public string Password { get; set; }
public int Age { get; set; }
}

var userFaker = new Faker<CustomUser>()
.RuleFor(u => u.Username, f => f.Internet.UserName())
.RuleFor(u => u.Password, f => f.Internet.Password(8, false, "[A-Za-z0-9]"))
.RuleFor(u => u.Age, f => f.Random.Int(18, 60));

var users = userFaker.Generate(3);

foreach (var user in users)
{
Console.WriteLine($"Username: {user.Username}, Password: {user.Password}, Age: {user.Age}");
}

Localized Data

var ukrainianFaker = new Faker("uk");

for (int i = 0; i < 5; i++)
{
string name = ukrainianFaker.Name.FullName();
string address = ukrainianFaker.Address.FullAddress();

Console.WriteLine($"Імя: {name}, Адреса: {address}");
}

Conclusion

The Bogus library in C# is a versatile and powerful tool for generating fake data for testing and development purposes. With its simple API, extensive support for various data types, and customization capabilities, Bogus can significantly streamline the process of creating realistic test data. By incorporating Bogus into your development workflow, you can improve the quality and reliability of your tests, leading to more robust and dependable software.

--

--

Valentyn Osidach 📚
.Net Programming

I am a software developer from Ukraine with more than 7 years of experience. I specialize in C# and .NET and love sharing my development knowledge. 👨🏻‍💻