When developing software, generating realistic fake data enables developers to simulate real-world scenarios without relying on sensitive or actual user information.
Fake data generation should accurately mimic fields such as names, addresses and dates, while also facilitating the creation of large datasets.
This is where Bogus, a powerful C# library, comes into play.
Bogus - The Most Popular Choice
Bogus is a simple yet powerful fake data generator for .NET languages. It's essentially a C# port of faker.js.
It is widely preferred because of its flexibility and data generation capabilities. It ensures realistic and consistent data, making it ideal for unit, integration and performance testing.
Bogus offers extensive support for generating a wide range of predefined data types, including names, addresses, phone numbers, emails, and dates. It also covers specialized categories like finance, commerce, the internet, and users.
Getting Started with Bogus
To get started with Bogus, you need to install the NuGet package. You can do this via the NuGet Package Manager or by running the following command in the Package Manager Console:
Install-Package Bogus
Basic Usage
Once installed, you can start generating fake data.
Using the Faker<T> class provided by Bogus, we set up rules for how each property should be populated with fake data using the RuleFor() method.
Here’s a simple example of generating fake user data with Bogus:
var orderFaker = new Faker<Order>()
.RuleFor(o => o.Id, _ => f.Random.Int())
.RuleFor(o => o.SubmittedDate, f => f.Date.Past(2))
.RuleFor(o => o.TotalPrice, f => f.Finance.Amount(50, 500));
var orders = orderFaker.Generate(3);
Its Fluent syntax makes defining data models straightforward, allowing you to chain method calls to configure complex object structures in a readable and intuitive way.
Nested Fake Objects
Bogus makes it simple to generate collections of nested fake objects as well.
var userFaker = new Faker<User>()
.RuleFor(u => u.Id, f => f.Random.Int())
.RuleFor(u => u.Name, f => f.Name.FullName())
.RuleFor(u => u.Email, f => f.Internet.Email())
.RuleFor(u => u.Orders, f => orderFaker.Generate(f.Random.Int(1, 5)));
Determinism
By setting a seed value, you ensure that the same data is produced each time the faker runs. Using Global Seed determinism is the easiest way to get deterministic data values over multiple executions.
// Using Global Seed Determinism
Randomizer.Seed = new Random(1234);
It's easy to get deterministic data but code changes can impact other data values. Using local seed determinism through Faker
// Using Local Seed determinism
var seededOrder = SeededOrder(1234);
Order SeededOrder(int seed)
{
return orderFaker.UseSeed(seed).Generate();
}
Localization Support
Bogus supports multiple locales, allowing you to generate data in different languages and formats. This is useful for testing internationalized applications:
var frenchFaker = new Faker(locale: "fr");
WriteLine($"French first name: {frenchFaker.Name.FirstName()}");
WriteLine($"French last name: {frenchFaker.Name.LastName()}");
Integration with Entity Framework Core
If you’re using Entity Framework Core, Bogus can help seed your database with realistic data. This can be particularly useful for testing and development environments:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var ids = 1;
var faker= new Faker<Product span>>()
.RuleFor(m => m.Id, f => ids++)
.RuleFor(m => m.Name, f => f.Commerce.ProductName())
.RuleFor(m => m.Category, f => f.Commerce.Categories(1).First())
.RuleFor(m => m.Price, f => f.Random.Decimal());
modelBuilder
.Entity<Product>()
.HasData(faker.Generate(1000));
}
Conclusion
Bogus is a versatile and powerful library for generating fake data in .NET applications that ensures realistic data for both small-scale and large-scale data simulations.
Its ability to handle complex types, support multiple locales and integrate with Entity Framework Core makes it an invaluable tool for developers.
Check out Bogus on GitHub and if you like it, give it a star:
GitHub: bchavez/BogusIf you want to check out examples I created, you can find the source code here:
Source CodeI hope you enjoyed it, subscribe and get a notification when new blog is up!
