DEV Community

Juarez Júnior for Develop4Us

Posted on • Edited on

1

Generating Fake Data for Testing with Bogus

Bogus is a popular library for generating fake data easily and efficiently in .NET. It is widely used to create realistic data for testing and development scenarios. Bogus can generate data for various types of information, such as names, addresses, dates, numbers, and more, allowing you to test your applications with diverse data. In this example, we will see how to use Bogus to generate a list of fake data.

Libraries:

To use the Bogus library, install the following NuGet package in your project:

Install-Package Bogus
Enter fullscreen mode Exit fullscreen mode

Example Code:

using Bogus;
using System;
using System.Collections.Generic;

namespace BogusExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Fake data generator for the Person class
            var faker = new Faker<Person>()
                .RuleFor(p => p.Name, f => f.Name.FullName())
                .RuleFor(p => p.Email, f => f.Internet.Email())
                .RuleFor(p => p.Age, f => f.Random.Int(18, 60))
                .RuleFor(p => p.Address, f => f.Address.FullAddress());

            // Generating a list of 5 people
            var people = faker.Generate(5);

            // Displaying the generated data in the console
            foreach (var person in people)
            {
                Console.WriteLine($"Name: {person.Name}, Email: {person.Email}, Age: {person.Age}, Address: {person.Address}");
            }
        }
    }

    // Class to represent a person
    public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

In this example, we use Bogus to generate fake data for the Person class. The generator (Faker) is configured with rules to automatically fill the Name, Email, Age, and Address properties of a person using predefined methods from the Bogus library. We then generate a list of 5 people and display the data in the console. This approach is useful for generating random data for testing and application development.

Conclusion:

Bogus is a powerful and flexible tool for generating realistic fake data in .NET. It simplifies creating test and development scenarios by automating the generation of realistic and varied data.

Source code: GitHub

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more