DEV Community

Cover image for How To Generate Fake Data With Bogus in .NET 6
Andy
Andy

Posted on

How To Generate Fake Data With Bogus in .NET 6

Bogus is a popular open-source library for generating fake data in .NET applications. It allows us to create realistic-looking, randomized test data for various purposes, such as unit testing, database seeding, or generating sample data for demonstrations.

In .NET 6, we can easily incorporate Bogus library into our projects by adding it as a NuGet package. Once installed, we can leverage its extensive set of data generation capabilities to create realistic and diverse fake data.


Key features and benefits

  • Data Population: Bogus provides methods for easily populating objects and collections with fake data. This is particularly useful for database seeding or creating large datasets for performance testing.

  • Realistic Data: The generated data from Bogus is designed to resemble real-world data. It follows patterns, distributions, and variations typically found in actual data, helping us create more accurate test scenarios and sample datasets.

  • Rich Data Generation: It provides a wide range of data generators that can create fake data for common types like names, addresses, phone numbers, email addresses, dates, numbers, and more. It also supports generating custom data types using fluent API extensions.

  • Localization Support: It supports generating data in multiple languages, making it suitable for internationalization testing or multi-lingual applications. It includes built-in localization for various countries and allows us to define our custom locales.

  • Customization Options: We can customize the generated data to match specific requirements. Bogus allows us to control the format, constraints, and patterns of the generated data, ensuring it aligns with our application’s needs.

  • Integration with Entity Framework Core: If you’re using Entity Framework Core in your .NET 6 project, Bogus includes built-in integration to help you generate fake data for your database entities and seed your database with realistic test data.


How to use

Install Nuget Package Bogus



Install-Package Bogus



Enter fullscreen mode Exit fullscreen mode

Let’s use a User as an example



public class User
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? LastName { get; set; }
    public string? Country { get; set; }
    public string? Phone { get; set; }
}


Enter fullscreen mode Exit fullscreen mode

Next step we just need to configure how the data will be generated.

The first parameter of the RuleFor method is a lambda to pick the property on User that we want to fake. The second parameter is another lambda to pass in how to generate the property.



public class UserRepository
{
    public List<User> GetSampleData()
    {
        var userId = 1;

        var userFaker = new Faker<User>()
            .RuleFor(u => u.Id, _ => userId++)
            .RuleFor(u => u.Name, f => f.Name.FirstName())
            .RuleFor(u => u.LastName, f => f.Name.LastName())
            .RuleFor(u => u.Country, f => f.Address.Country())
            .RuleFor(u => u.Phone, f => f.Phone.PhoneNumberFormat());

        var users = userFaker.Generate(1000);

        return users;
    }
}


Enter fullscreen mode Exit fullscreen mode

To use the Faker, we just need to call Generate method with how many users we want.

Image description

As we can see 1000 users were generated using realistic names. This is because Bogus contains a set of generalized rules for common data categories (i.e. Names, Addresses, Companies, People, Phone Numbers, etc.)

Image description

With Bogus, we can generate data in more than 45 languages.

Image description

We just need to pass the locale code inside the constructor.



var userFaker = new Faker<User>("es")



Enter fullscreen mode Exit fullscreen mode

Image description

For more info https://github.com/bchavez/Bogus

Happy Coding :)


Final Thoughts

Overall, Bogus is a powerful tool that simplifies the process of generating fake data in .NET 6 applications. It enables us to create diverse, realistic, and customizable test data, saving us time and effort in the development and testing process.


Thanks for reading!

Through my articles, I share Tips & Experiences on web development, career, and the latest tech trends. Join me as we explore these exciting topics together. Let’s learn, grow, and create together!

➕More article about Programming, Careers, and Tech Trends.

🔔 Follow me on Medium | Twitter

Top comments (0)