Introduction to Bogus - A Fake Data Generation Library
Why we need to generate fake data
While working with our applications we need to generate fake data for different purposes.
- For testing purpose (especially unit testing)
- For demonstration of data maybe for charts/graphs
- Dumping the data in databases
What is the Bogus and benefits of using it
If you come from JS world then you would be aware about faker.js Bogus is C# version of that library.
Bogus is a Nuget package available on Nuget Package Manager with approximately 46M downloads.
These are the benefits of Bogus :
-
Compatible with C# , F# and VB.NET
-
Supported in 50 international languages
-
It comes with a variety of data e.g. addresses,commerec,companies,database,finance,dates,images,vehicles and many more
-
It has some country based extensions as well for different countries (e.g. Brazil,Norway,Finland,Italy and few more)
-
It has premium packages as well for movies,tvs,literature,medical and diagnosis details
-
It gives us the facility to apply variety pf rules on data according our need
-
Its really simple to use it and you don’t need any lengthy process , just install package and use it
How to use it in .NET Core
-
Step 1 : Install Nuget Package with name Bogus (With 45.3 M Downloads)
-
Step 2 : Make rules for your fake model
-
Step 3 : Call Generate method of library to generate data
Let’s see with code now for better understanding
Simple Demonstration of Bogus
In this example you can check all list of properties/methods available in library in second part of expression(RuleFor)
using Bogus;
using System.Text.Json;
// Step 1: Define rules
var fakeDotNetDeveloper = new Faker<Student>()
.RuleFor(s => s.Id, f => f.IndexFaker)
.RuleFor(s => s.FullName, f => f.Name.FullName())
.RuleFor(s => s.Country, f => f.Address.Country());
// Step 2: Call generate method
var fakeDeveloper = fakeDotNetDeveloper.Generate();
var json = JsonSerializer.Serialize(fakeDeveloper);
Console.WriteLine(json);
public class Student
{
public int Id { get; set; }
public string? FullName { get; set; }
public string? Country { get; set; }
}
How to Generate N’ Fake Entries
We can pass an integer in parameter to Generate method and it will create that much entries.
var fakeDeveloper = fakeDotNetDeveloper.Generate(2);
var json = JsonSerializer.Serialize(fakeDeveloper);
Console.WriteLine(json);
The result for above Console.Writeline command would be this
[
{
"Id": 0,
"FullName": "Jon Beier",
"Country": "Vietnam"
},
{
"Id": 1,
"FullName": "Boris Cassin",
"Country": "Qatar"
}
]
How to Generate Same Data Every Time
Bogus gives us the facility to create same data every time by defining a random number of our choice like this .
// To generate similar data all time
Randomizer.Seed = new Random(8675309);
var fakeDotNetDeveloper = new Faker<Student>()
.RuleFor(s => s.Id, f => f.IndexFaker)
.RuleFor(s => s.FullName, f => f.Name.FullName())
.RuleFor(s => s.Country, f => f.Address.Country());
How to Generate Data in Other Languages
var fakeDotNetDeveloper = new Faker<Student>("fr")
.RuleFor(s => s.Id, f => f.IndexFaker)
.RuleFor(s => s.FullName, f => f.Name.FullName())
.RuleFor(s => s.Country, f => f.Address.Country());
By default data is generated in “en” but you can pass language code of your choice to generate data in your language . Check language list from Bogus GitHub Repo
This is a just basic overview of library , covering each and everything is not possible , I would recommend you to visit GitHub repository of the library and you can find the things which you need.
Whenever you're ready, there are 3 ways I can help you:
- Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
- Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
- Patreon community : Get access to all of my blogs and articles at one place