Library: Bogus

Fake data generator

Published on Monday, March 7, 2022

We often work on projects or parts of the system where we expect the typical user to have hundreds or thousands of entries. Still, we always test the development system with only a few representative entites because we don't want to create realistic data sets. If we don't expect production data to impact performance and stability negatively, we will not generate dummy data.

Maybe it would be better to start with more realistic representative data. But, how to make it less tedious?

Bogus to the rescue!

Bogus is a simple fake data generator for .NET.

The bogus library is based on faker.js, a popular framework for javascript, created by Brian Chavez. It has autogenerating features for many types of data, including Person, Address, Company, Finance, Internet, Images, Lorem, Names, Reviews, Vehicle, etc. With Bogus Premium (9.99$ per developer annually), it is possible to generate Healthcare data, Hollywood, Advanced Locations, Historical Texts, etc.

Requirements (as of 2022/02): .NET Standard 1.3 or .NET Standard 2.0 or .NET Framework 4.0.

At the time of writing GitHub project has 5.3k stars and NuGet library 21.2M downloads.

Give it a star on GitHub!

Bogus Usage

The setup is self-explanatory. Fixed seed ensures you will always get the same data.

 public static List < Person > GenerateSeedData(int total = 100, bool randomizedData = true) {
   if (!randomizedData)
     Randomizer.Seed = new Random(_seed);

   var testUsers = new Faker < Person > ()
     .RuleFor(x => x.Id, f => f.Random.Guid())
     .RuleFor(x => x.Gender, f => (Gender) f.Person.Gender)
     .RuleFor(x => x.Name, (f, x) => f.Person.FirstName)
     .RuleFor(x => x.Surname, (f, x) => f.Person.LastName)
     .RuleFor(x => x.Email, f => f.Person.Email)
     .RuleFor(x => x.Company, f => f.Person.Company.Name + " " + f.Company.CompanySuffix())
     .RuleFor(x => x.Address, f => f.Person.Address.State + " " + f.Person.Address.City)
     .RuleFor(x => x.Phone, f => f.Person.Phone)
     .FinishWith((f, u) => {
       Console.WriteLine("User Created! Id={0}", u.Id);
     });

   return testUsers.Generate(total);
 }

One exciting thing about the result is that when bogus creates a fake person, it generates all data, so even the email looks realistic because it's built from the first and second names.

 {
    "Id": "2705d46e-7811-6d8f-de20-fb8634a2924b",
    "Gender": "Female",
    "Name": "Rosalie",
    "Surname": "Ernser",
    "Email": "Rosalie.Ernser@gmail.com",
    "Company": "Mann - Homenick LLC",
    "Phone": "961-906-3044 x74404",
    "Address": "Maine New Regantown"
  }

Check out Nick Chapsas' Bogus video
https://www.youtube.com/watch?v=T9pwE1GAr_U