Skip to content

What it is

Bogus generates realistic fake data for .NET — names, addresses, emails, dates and text — for tests, seeding and demonstrations.

Define a Faker<T> describing how to populate each property, then generate one or many. Seeding makes output reproducible.

Installation

dotnet add package Bogus

Getting started

The smallest useful thing you can do with it, and what each part means.

A reproducible generator
var faker = new Faker<Book>()
    .UseSeed(42)                      // deterministic across runs
    .RuleFor(b => b.Id, f => f.IndexFaker + 1)
    .RuleFor(b => b.Title, f => f.Lorem.Sentence(3).TrimEnd('.'))
    .RuleFor(b => b.Year, f => f.Random.Int(1950, 2026))
    .RuleFor(b => b.Isbn, f => f.Commerce.Ean13())
    .RuleFor(b => b.AddedAt, f => f.Date.Past(2))
    .RuleFor(b => b.Author, f => new Faker<Author>()
        .RuleFor(a => a.Name, x => x.Name.FullName())
        .Generate());

var one = faker.Generate();
var many = faker.Generate(1000);
UseSeed is what makes this safe in tests: the data looks realistic but is identical on every run, so a failure is reproducible.

Advanced usage

Where the library earns its place over a simpler alternative.

Conditional and weighted rules
var faker = new Faker<Order>()
    .RuleFor(o => o.Status, f => f.PickRandom<OrderStatus>())
    // 5% of orders have no email — exercise the null path.
    .RuleFor(o => o.Email, f => f.Random.Bool(0.95f) ? f.Internet.Email() : null)
    .RuleFor(o => o.ShippedAt, (f, o) =>
        o.Status == OrderStatus.Shipped ? f.Date.Recent(10) : null)
    .FinishWith((f, o) => logger.LogDebug("generated {Id}", o.Id));

// Locale-specific data for internationalisation testing.
var japanese = new Faker<Customer>("ja")
    .RuleFor(c => c.Name, f => f.Name.FullName());
Deliberately generating nulls and inconsistent states is the point — uniformly perfect fake data hides exactly the bugs that real data will find.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Tests fail intermittently
Unseeded randomness. Call UseSeed, and log the seed if you randomise it deliberately.
Generated data violates a database constraint
Bogus does not know your constraints. Add rules matching them, for example unique emails via f.IndexFaker.

Best practices

  • Always UseSeed in tests so failures are reproducible.
  • Generate some invalid and missing values on purpose; perfect data tests nothing.
  • Keep generators next to the tests that use them rather than in one shared file.
  • Use locale-specific fakers when testing internationalisation and field lengths.

Background

Why it exists, and what it was reacting to.

A port of JavaScript's faker.js, Bogus replaced the "test1, test2" placeholder data that makes demos look unfinished and hides bugs that only appear with realistic input.