Seeding is a necessary evil for a lazy developer every day. When unit testing, we are messing with what I should use for names, emails, paragraphs, etc. With Bogus, they become slightly more manageable.
Bogus is a lovely library from Brian Chavez to use in automated tests to generate test data of different kinds automatically. Brian says,
"A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js."
In my day-to-day coding life, I work with Proof Of Concepts (PoCs), Sample projects for my blog posts, and research tasks. For them, Bogus is the more robust data generator for me, with a realistic looking at the data.
Here is the Bogus GitHub repository link. Please make ⭐ a star ⭐ for the repository. It will encourage the author.
Popularity
Criteria | Counts |
---|---|
GitHub Stars | 6.3k |
GitHub Forks | 380 |
Github Issues | 23 |
NuGet Downloads | 32.6M |
How to install
You can easily install it using the NuGet package manager console using.
NuGet Package
Install-Package Bogus
As well as .Net CLI
dotnet add package Bogus
Examples
Here I am creating a Dotnet console application using .Net 6. Then I add Bogus NuGet dependency using the above command.
Here I am creating a Customer POCO class that fills the data randomly.
The plan is to seed realistic Customer data to the Customer object.
In this seeding journey, we should first create a faker object via our POCO class. Then map our properties using the Rules concept like in the FluenntValidator library. The property of the Review object may be provided as the first argument to the RuleFor()
methods, and the second argument indicates how the property value should be generated.
Here we generate an address list like a sub-class for the main Customer
class. It is another faker object. The rest of both fakers are generating individually at once.
Then we can generate our Customer
data and Address
data using Generate()
the method.
We can then serialize this object into JSON and write it to the console:
This blog post is recommended to refer with marvelous and informative.
Supported Entities by Bogus
There is a vast range of test data types supported. Below I put some supported areas and properties. Apart from this, you can get a massive idea from here | Bogus Documentation
# | Entity | Properties |
---|---|---|
1 | Address | ZipCode, City, Country, Latitude, etc |
2 | Commerce | Department name, ProductName, ProductAdjective, Price, etc |
3 | Company | CompanyName, CatchPhrase, Bs, etc |
4 | Database | Column, Type,Collation etc |
5 | Date | Past, Soon, Between, etc |
6 | Finance | Account number, TransactionType, Currency, CreditCardNumber, etc |
7 | Hacker | Abbreviation ,Adjective etc |
8 | Images | Random image, Animals image, Nature image, etc. |
9 | Internet | Email, DomainName, Ipv6, Password, et |
10 | Lorem | single word, Words, Sentence, Paragraphs, etc. |
11 | Name | FirstName, LastName, etc |
12 | Phone | PhoneNumber , PhoneNumberFormat |
13 | Rant | Random user review, etc |
14 | System | FileName, MimeType, FileExt, etc. |
15 | Vehicle | Manufacturer , Model , etc |
16 | Random/Randomizer | Number, Digits, Hash, String, etc |
Summary
A fluent interface into a world of random realistic data, Bogus is a mandatory tool for any programmer. Please give him a star for his contribution and share your thoughts via tweet.
Source code - All in one
Repository Link
Learn something new. Comment your thoughts and share the content.
Happy coding!!
See you again soon. ✌✌