Skip to main content

Command Palette

Search for a command to run...

Stop Hand-Crafting Test Data — Automate It With Bogus in C#

Updated
4 min read
A

I am Abubakar Riaz, a Principal DevOps Engineer at Tkxel, a software development company that provides custom solutions for startups and enterprises. I have over 13+ years of experience in the IT sector mainly telecom, where I implemented CI/CD, orchestrated data marts, and automated business flows using various tools and technologies. I am currently pursuing my M. Phil degree in Computer Science and hold multiple certifications in .Net, API, Azure, and project management.

I am passionate about creating enterprise-level progressive web apps, improving web UI and UX, and designing microservices architecture for complex systems. I have also worked as a Solutions Architect for Punjab Group, an education management company, where I designed and deployed their microservices-based education management system using Azure DevOps, C# 10, T-SQL, VS 2022, PostgreSQL, and Docker. In addition, I am a professional Microsoft database trainer at EVS Institute and a part-time Automation advisor at Magnus Digital Marketing. I enjoy learning new skills, sharing my knowledge, and working on Raspberry Pi projects in my spare time.

If you’ve ever spent more time writing fake users, orders, or sample records than building features — you’re not alone. Generating reliable, realistic dummy data manually is tedious, error-prone, and gets in the way of shipping software. Enter Bogus — a .NET native fake data generator inspired by faker.js that lets you declaratively define data rules and spin up thousands of realistic records in seconds. (GitHub)


🧪 Why Bogus Is the Best Choice for Dummy Data in .NET

Bogus is one of the most popular dummy data libraries in the .NET ecosystem:
✔️ C#, F#, and VB.NET support — integrates seamlessly with .NET apps and test projects. (GitHub)
✔️ Fluent rule configuration — define per-property generation logic with expressive RuleFor calls. (GitHub)
✔️ Locale-aware realistic data — names, cities, addresses, emails, GUIDs and more in many locales. (GitHub)
✔️ Repeatability — deterministic seed support makes tests reliably reproducible. (GitHub)

Compared to hand-rolled randomization or basic loops, Bogus gives:

  • Consistent, human-looking values (not just random characters),

  • Structured object graph generation,

  • Plug-and-play usage with EF Core seeders and test fixtures,

  • Support for constraints (e.g., emails tied to names, or enums randomly assigned).


🚀 Core Concepts — Bogus in a Nutshell

At its heart, Bogus uses a fluent API to define fake data rules per property on your POCO model. You then ask it to Generate instances — either singly or in bulk.

Basic components:

  • Faker<T> – core generator for model T

  • RuleFor() – define how each property should be populated

  • Optional global or local Randomizer.Seed to make outputs repeatable


📌 Example: Fake Orders

Here’s a simple example that creates fake Order objects:

public class Order {
    public int OrderId { get; set; }
    public string Item { get; set; }
    public int Quantity { get; set; }
}

Randomizer.Seed = new Random(8675309);

var fruit = new[] { "apple", "banana", "orange" };

var orderFaker = new Faker<Order>()
    .StrictMode(true)
    .RuleFor(o => o.OrderId, f => f.IndexFaker++)  
    .RuleFor(o => o.Item, f => f.PickRandom(fruit))
    .RuleFor(o => o.Quantity, f => f.Random.Number(1, 10));

var orders = orderFaker.Generate(5);

Output will look like real, structured data instead of gibberish — and all in a few lines. (GitHub)


🧑‍💻 Example: Fake Users With Relationships

Bogus shines when fake object graphs matter — for example, users with nested orders:

public class User {
    public int Id { get; set; }
    public string Email { get; set; }
    public string FullName { get; set; }
    public List<Order> Orders { get; set; }
}

var userFaker = new Faker<User>()
    .RuleFor(u => u.Id, f => f.IndexFaker++)
    .RuleFor(u => u.FullName, f => f.Name.FullName())
    .RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FullName))
    .RuleFor(u => u.Orders, f => orderFaker.Generate(3).ToList());

var user = userFaker.Generate();

Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(user, Formatting.Indented));

This generates a user and three associated orders, all realistic and test-ready. (GitHub)


🏁 Best Practices

  • Use strict mode (.StrictMode(true)) to ensure every property has a rule. (GitHub)

  • Local seed per faker for deterministic outputs in unit tests. (GitHub)

  • Nest faker rules for object graphs (lists, child objects). (GitHub)

  • Use extensions like AutoBogus if you want automatic rule inference without enumerating every property. (GitHub)


📦 Get Started

Add the package via NuGet:

Install-Package Bogus

Minimum .NET Standard 1.3 support means it works from .NET Framework 4.x up through modern .NET 8+ apps. (GitHub)


🧠 Conclusion

For C# developers who need realistic dummy data — for UI mockups, integration tests, or database seeds — Bogus is your best choice. Its fluent API, rich dataset generators, locale support, and deterministic options make it far superior to ad-hoc random data or simple Faker implementations. Whether you’re a beginner writing your first API or a seasoned architect designing EF Core seeders or automated tests, Bogus will save time and produce better quality data — consistently.