What is Bogus and why is it needed?
\n\nBogus is a powerful, simple, and fast library for generating fake (test) data in C# (.NET). It is a direct port of the popular JavaScript library Faker.js. The main goal of Bogus is to free developers from manually writing boilerplate code to create test data sets.
\n\nWhy is this needed? Imagine you are developing an application that works with users, orders, products, or any other entities. For testing functionality (unit tests, integration tests, load testing), you constantly need data: names, addresses, emails, phone numbers, text, numbers, dates, etc. Manually creating 1000 User objects with different data is tedious, time-consuming, and error-prone. Bogus solves this problem in one line of code.
Key advantages of Bogus:
\n- \n
- Localization: Support for dozens of languages and regional formats (ru, en, de, fr, etc.). \n
- Flexibility: You can generate data considering business rules (e.g., "price must be greater than 0 and less than 1000"). \n
- Repeatability: Using a seed, you can get the same data sets on every run — critically important for reproducible tests. \n
- Performance: Bogus is optimized to generate millions of records in seconds. \n
- Type Safety: Works with your own classes and interfaces using C#'s strong typing. \n
Installing Bogus
\n\nBogus is distributed as a NuGet package. It can be installed in several ways:
\n\n1. Via .NET CLI (recommended)
\ndotnet add package Bogus\n\n2. Via Package Manager Console (Visual Studio)
\nInstall-Package Bogus\n\n3. Via the NuGet Package Manager interface in Visual Studio
\nOpen "Manage NuGet Packages", find "Bogus", and click "Install".
\n\nAfter installation, add a using directive to your code:
\nusing Bogus;\n\nQuick Start: First Example
\n\nLet's generate a simple collection of Person objects with Russian names and addresses.
using Bogus;\n\n// 1. Create "rules" (Faker) for data generation\n// Specify the "ru" locale for Russian data\nvar faker = new Faker("ru");\n\n// 2. Generate 10 fake persons\nfor (int i = 0; i < 10; i++)\n{\n var person = new\n {\n Name = faker.Name.FullName(), // Full name\n Email = faker.Internet.Email(), // Email\n Phone = faker.Phone.PhoneNumber(), // Phone number\n City = faker.Address.City(), // City\n Age = faker.Random.Int(18, 80) // Age from 18 to 80\n };\n\n Console.WriteLine($"{person.Name}, {person.Age} years old, {person.City}, {person.Email}, {person.Phone}");\n}\n\n// Example output (will change on each run):\n// Ivanov Ivan Petrovich, 34 years old, Moscow, ivanov@example.com, +7 (495) 123-45-67\n// Petrova Anna Sergeevna, 27 years old, Saint Petersburg, petrova@test.ru, +7 (812) 987-65-43\n// ...\n\nHow does this work? new Faker("ru") creates the main generator object. Its properties Name, Internet, Phone, Address, Random are built-in "categories" (APIs), each containing dozens of methods for generating specific data.
Main Methods and Functions of Bogus
\n\nBogus provides a huge number of methods. Below are 10+ of the most commonly used categories and their key methods with detailed examples.
\n\n1. Name — Generating Names
\nWhat it does: Generates first names, last names, patronymics (for Russian locale), full names, prefixes, and suffixes.
\nParameters:
Also in library
Recommendations