| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
Introduction to Bogus - A Fake Data Generation Library
May 20, 2023
3 min read

Introduction to Bogus - A Fake Data Generation Library

Sponsor this Newsletter

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 :

  1. Compatible with C# , F# and VB.NET

  2. Supported in 50 international languages

  3. It comes with a variety of data e.g. addresses,commerec,companies,database,finance,dates,images,vehicles and many more

  4. It has some country based extensions as well for different countries (e.g. Brazil,Norway,Finland,Italy and few more)

  5. It has premium packages as well for movies,tvs,literature,medical and diagnosis details

  6. It gives us the facility to apply variety pf rules on data according our need

  7. 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.

This article was originally published at https://mwaseemzakir.substack.com/ on May 20, 2023 .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

© 2025 Muhammad Waseem. All rights reserved.