Here’s How to Use Bogus to Populate Your Test Data

Creating realistic test data is essential for effective testing and development, but manually generating it can be tedious and time-consuming. A fake data generation library like Bogus streamlines your workflow and ensures your tests use realistic-looking data.

What Is Bogus?

Bogus is a .NET library that generates fake data for tests. It offers a wide variety of built-in methods to generate different types of data, such as names, addresses, phone numbers, placeholder URLs, blocks of lorem ipsum, and more. Bogus supports multiple locales, allowing you to generate data in various languages. Its flexibility, lightweight design, and ease of use make it an excellent tool for setting up unit tests, populating databases, or creating mock objects for UI testing.

How To Use Bogus

To start using Bogus, you first have to install it. You can do this with either dotnet or NuGet.

  • Using dotnet: dotnet add package Bogus
  • Using NuGet: Install-Package Bogus

Once Bogus is installed, you are ready to start generating fake data. Start by initializing a faker object. A faker will allow you to call a variety of different methods that will generate fake data for you.

var faker = new Faker();
var name = faker.Name.FullName();
var address = faker.Address.FullAddress();
var email = faker.Internet.Email();
var phoneNumber = faker.Phone.PhoneNumber();

Bogus also allows for generating data for specific objects which can be very useful for generating fake test data for a large object quickly instead of having to build up each item within a test case by hand.

public class Program
{
   public static void Main(string[] args)
   {
       var userFaker = new Faker()
           .RuleFor(u => u.FullName, f => f.Name.FullName())
           .RuleFor(u => u.Email, f => f.Internet.Email()) 
           .RuleFor(u => u.DateOfBirth, f => f.Date.Past(30, DateTime.Now.AddYears(-18)))
           .RuleFor(u => u.Address, f => f.Address.FullAddress())
           .RuleFor(u => u.PhoneNumber, f => f.Phone.PhoneNumber());
    }
}

public class User
{
    public string FullName { get; set; }
    public string Email { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
}

Once a faker has been created with rules for the object you want to populate, you can call the generate method on the faker to generate one or more instances of the target object populated with data.

var oneUser = userFaker.Generate();
var multipleUsers = userFaker.Generate(10);

When using Bogus in unit tests, you may need the faker to generate the same fake data each time a test runs to verify the functionality tested. To achieve this, set the randomizer seed within the faker. You can set the seed globally or locally, but it’s better to use a local seed for unit tests. Global seeds can be affected by code changes, which can make test data unreliable and force you to spend more time updating tests after making changes to your code.

// Set global seed
Randomizer.Seed = new Random(123);

// Set local seed
var faker = new Faker.UseSeed(123);

Bogus gives you a quick and easy way to generate fake data for tests in your .NET project. Generating realistic test data quickly saves time while providing meaningful datasets that improve the quality and reliability of your tests. Try it out, and see how it streamlines your testing process and makes your development workflow more efficient!

Conversation

Join the conversation

Your email address will not be published. Required fields are marked *