Bogus
:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Install / Use
/learn @bchavez/BogusREADME
<img src="https://raw.githubusercontent.com/bchavez/Bogus/master/Docs/logo.png" align='right' />
Bogus for .NET: C#, F#, and VB.NET
Project Description
Hello. I'm your host Brian Chavez (twitter). Bogus is a simple fake data generator for .NET languages like C#, F# and VB.NET. Bogus is fundamentally a C# port of faker.js
and inspired by FluentValidation's syntax sugar.
Bogus will help you load databases, UI and apps with fake data for your testing needs. If you like Bogus star :star: the repository and show your friends! :smile: If you find Bogus useful consider supporting the project by purchasing a Bogus Premium license that gives you extra Bogus superpowers! :dizzy: :muscle: You can also sponsor the project here! :moneybag: :dollar:
Download & Install
Nuget Package Bogus
Install-Package Bogus
Minimum Requirements: .NET Standard 1.3 or .NET Standard 2.0 or .NET Framework 4.0.
Projects That Use Bogus
- Bitwarden Passwordless Server [code]
- Elasticsearch .NET Client (NEST) [code]
- FluentValidation [code]
- Microsoft Windows-XAML / Template10 [code]
- Microsoft Learning / Cloud-Native Applications Using Microsoft Azure Cosmos DB [code]
- Microsoft Office Developer
Featured In
- Amazon AWS .NET Open Source Fund Recipient - Oct. 1st, 2024
- Microsoft 'On .NET Live': Next-level EF Core Data Seeding with Bogus - Apr. 15th, 2024
- Adventures in .NET: Building Bogus with Brian Chavez - Jan 5th, 2024
- Microsoft Docs: Deploy Orleans to Azure App Service - May 5th, 2022
- Microsoft Docs: Modernize a WPF app tutorial - Aug. 2nd, 2019
- Microsoft Build 2018: Azure Tips and Tricks - May 8th, 2018
- NuGet Must Haves: Top 10 Unit Testing Libraries in 2017
- .NET Rocks Podcast: #BetterKnowThatFramework - Mar. 16th 2017, Sep. 13th, 2018, and May 7th, 2020
- .NET Engineering Blog: NuGet Package of the week #1. - "This week in .NET - December 8th 2015"
Blog Posts
- Nick Chapsas - Generating realistic fake data in .NET using Bogus [YouTube]
- Jack Histon - How to Create Bogus Data in C#
- Christos Matskas - Creating .NET fakes using Bogus
- Jason Roberts - Lifelike Test Data Generation with Bogus
- Mark Timmings - Auto generating test data with Bogus
- .NET Core Generating Test Data
- Steve Leigh - Seedy Fake Users
- Volkan Paksoy - Generating Test Data with Bogus
- Alican Demirtas - Bogus on F#
- Khalid Abuhakmeh - Seed Entity Framework Core With Bogus
- Sander ten Brinke - Taking Entity Framework Core data seeding to the next level with Bogus
The Crypto Tip Jar!
<a href="https://commerce.coinbase.com/checkout/2faa393a-6fc3-4365-993a-6cc110bc4d35"><img src="https://raw.githubusercontent.com/bchavez/Bogus/master/Docs/tipjar.png" /></a>
Usage
The Great C# Example
public enum Gender
{
Male,
Female
}
//Set the randomizer seed if you wish to generate repeatable data sets.
Randomizer.Seed = new Random(8675309);
var fruit = new[] { "apple", "banana", "orange", "strawberry", "kiwi" };
var orderIds = 0;
var testOrders = new Faker<Order>()
//Ensure all properties have rules. By default, StrictMode is false
//Set a global policy by using Faker.DefaultStrictMode
.StrictMode(true)
//OrderId is deterministic
.RuleFor(o => o.OrderId, f => orderIds++)
//Pick some fruit from a basket
.RuleFor(o => o.Item, f => f.PickRandom(fruit))
//A random quantity from 1 to 10
.RuleFor(o => o.Quantity, f => f.Random.Number(1, 10))
//A nullable int? with 80% probability of being null.
//The .OrNull extension is in the Bogus.Extensions namespace.
.RuleFor(o => o.LotNumber, f => f.Random.Int(0, 100).OrNull(f, .8f));
var userIds = 0;
var testUsers = new Faker<User>()
//Optional: Call for objects that have complex initialization
.CustomInstantiator(f => new User(userIds++, f.Random.Replace("###-##-####")))
//Use an enum outside scope.
.RuleFor(u => u.Gender, f => f.PickRandom<Gender>())
//Basic rules using built-in generators
.RuleFor(u => u.FirstName, (f, u) => f.Name.FirstName(u.Gender))
.RuleFor(u => u.LastName, (f, u) => f.Name.LastName(u.Gender))
.RuleFor(u => u.Avatar, f => f.Internet.Avatar())
.RuleFor(u => u.UserName, (f, u) => f.Internet.UserName(u.FirstName, u.LastName))
.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName))
.RuleFor(u => u.SomethingUnique, f => $"Value {f.UniqueIndex}")
//Use a method outside scope.
.RuleFor(u => u.CartId, f => Guid.NewGuid())
//Compound property with context, use the first/last name properties
.RuleFor(u => u.FullName, (f, u) => u.FirstName + " " + u.LastName)
//And composability of a complex collection.
.RuleFor(u => u.Orders, f => testOrders.Generate(3).ToList())
//Optional: After all rules are applied finish with the following action
.FinishWith((f, u) =>
{
Console.WriteLine("User Created! Id={0}", u.Id);
});
var user = testUsers.Generate();
Console.WriteLine(user.DumpAsJson());
/* OUTPUT:
User Created! Id=0
*
{
"Id": 0,
"FirstName": "Audrey",
"LastName": "Spencer",
"FullName": "Audrey Spencer",
"UserName": "Audrey_Spencer72",
"Email": "Audrey82@gmail.com",
"Avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/itstotallyamy/128.jpg",
"CartId": "863f9462-5b88-471f-b833-991d68db8c93",
"SSN": "923-88-4231",
"Gender": 0,
"Orders": [
{
"OrderId": 0,
"Item": "orange",
"Quantity": 8
},
{
"OrderId": 1,
"Item": "banana",
"Quantity": 2
},
{
"OrderId": 2,
"Item": "kiwi",
"Quantity": 9
}
]
} */
More Examples!
| Language | Description | |:---------:| ----------- | | C# | [**Full working exam
Related Skills
notion
335.2kNotion API for creating and managing pages, databases, and blocks.
feishu-drive
335.2k|
things-mac
335.2kManage Things 3 via the `things` CLI on macOS (add/update projects+todos via URL scheme; read/search/list from the local Things database)
clawhub
335.2kUse the ClawHub CLI to search, install, update, and publish agent skills from clawhub.com
