Using Bogus Framework to create good sample data in .Net Core

Whenever you create a unittest or just need some sample data, you probably begin by making up a couple of values. Your test UI-screen looks nice, everything is lined up perfectly fine. And then when your program goes live, one user has a really long name and another one has a name with a diacritic (you know e.g. a ç) and your UI looks weird.

No worries, you fix that case and even think of another one in the progress. Suddenly you have written out quite some lines of sample data and how many cases did you miss? Do you maybe need more?

Enter Bogus Framework: a framework specifically designed to create sample data. And the great part is: it's very easy to use.

In this small tutorial, I will show in a few steps how you can easily get realistic locale-specific sample data in your .net core program with a few lines of code and Bogus.

So let's start by creating a new .net core application.

1. Pick "Create a new project" in Visual Studio. I used Visual Studio 2019 Preview, but you could use any IDE.

select .net core project

2. Go for Console App (.NET Core) and click next.

create a console app

3. Pick a name for your Console App - I chose BogusDemo.

choose a project and solution name

4. Wait for the solution and project to be created and right click on the project / dependencies and click "Manage NuGet Packages ..."

search dependency

5. Now search for Bogus and install the package

add Bogus to your project

6. And when this is done, lets add some code. First of let's create a resident class in the file Resident.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace BogusDemo
public class Resident
public string Name { get; set; }
public string Id { get; set; }
public string Street { get; set; }
public string Housenumber { get; set; }

public string City { get; set; }

public void Print()
Console.WriteLine($"{Name} {Street} {Housenumber} {City} {Id}");

7. Now adjust the program.cs file into the following:

using Bogus;
using System;

namespace BogusDemo
class Program
static void Main(string[] args)

var resident = new Resident { Name = "Jan van Bommelen", Street = "Rijksweg", Housenumber = "3", City = "AAAA" };



If you'd run the above - there is no Bogus magic happening yet. There is just one resident created with made up values. On purpose I chose a City name that's just a display of lack of creativity. Also I figured it would be nice to use a Dutch name and address. If you run this program, by pressing F5, you will see:


8. Enter Bogus, no more need to make up data. Replace the code in the Main() by the following:

var residents = new Faker()
.Rules((f, o) =>
o.Id = f.Random.Replace("###-###-###-###");
o.Name = f.Name.FullName();
o.Street = f.Address.StreetName();
o.Housenumber = f.Address.BuildingNumber();
o.City = f.Address.City();
var residentList = residents.Generate(10);
foreach (var res in residentList)

This code sets up Bogus. You tell it that you want a Faker object of Type Resident. With the Rules here you define what values the properties of Resident-instances can have when they are generated

After defining the faker object - you call its Generate method. And now Bogus shows its skills. Because, if we run this program we see the following result:

Jan van Bommelen Rijksweg 3 AAAA
Charlotte Jacobs Torphy Mill 60688 Bucktown 082-956-150-133
Maximo Fadel Karelle Pine 40 East Hayden 009-791-457-155
Josiane Fisher Cecilia Turnpike 6729 Jarrodstad 703-502-047-450
Tatyana Kovacek D'Amore Court 263 West Owenside 128-679-685-998
Santino Bechtelar Welch Fall 223 Dickensland 548-418-489-901
Noemi Kiehn Arlo Light 954 East Oswaldostad 510-382-181-714
Christopher Beatty Schmidt Expressway 181 Lake Joannyville 710-055-527-998
Noemie McClure Lilla Flats 54229 Lake Eladioton 830-664-926-010
Ivah Adams Kuhlman Ville 7440 Port Carlotta 094-535-872-560
Kaycee Schamberger Saige Track 169 South Millermouth 209-548-833-832

Of course, this looks awesome already. On the other hand, didn't I want Dutch names and addresses? That is true and it did that on purpose: Because changing this in Bogus is sooo easy.

9. If you want Dutch names and addresses, just add a constructer argument to the Faker. The Dutch locale is "NL" (short for Netherlands).

The line becomes:

var residents = new Faker<Resident>("nl")

and this creates the following output:

Alev Haan Hendriksplein 931 III Overeysdonk 561-904-777-988
Joris Bakker Ulrichvelt 361b Burger 519-644-731-853
Vera Jong Judasplantsoen 46 Santfort 384-981-101-084
Annette Veen Kleinplein 97 Graetenmaes 419-446-600-169
Miles Bakker Helènepark 107a Aldegae 641-521-471-353
Anton Groot Othmanhof 456c Jistrumlaar 832-823-602-173
Tamira Graaf Nicolaspark 326a Haamstede 464-433-268-422
Adelheid Vliet Birgithof 045 II Wachtumheide 430-142-247-963
Marniks Brouwer Heuvelvelt 4 Heerszijl 630-027-198-390
Stefan Bos Jasongracht 7 Lagekant 553-633-617-445

This would most definetly pass as valid Dutch data. Also note the è in the streetname Helènepark. That would be something that manually created data might have forgotten to include.

10. But if you want to make testscripts, you might want to create the same "random" bogustestset every time you run your tests. This too can be done easily, just add a randomseed: When you setup the Faker, add the following line:


I randomly picked 300 - It could have been almost any number. And the code now will look like:

var residents = new Faker<Resident>("nl")

Now the output will be, the same time everytime you run this program:

Marijke Koster Hoekplein 746 III Raakvliet 484-567-390-836
Silvijn Ven Graafdijk 7 Beugt 148-546-949-537
Rokus Brouwer Smitrijk 987a Coevordenburen 414-829-662-016
Frederick KoningAndrémarkt 236 Helkant 812-466-647-107
Gerardus Meijer Sannavelt 683 Darplaar 540-532-220-020
Willy Bos Jan-Willempark 068 II Haar 999-041-759-770
Morgens Groot Kimberlyhof 603 Bierum 363-509-094-245
Louis Beek Petersvelt 827 III Meemortelwoude 359-434-047-465
Edwin Jong Jongsteeg 607 II Waddinx 820-042-494-600
Dirk Janssen Bakkerrijk 7 Roond 745-914-621-905

And with that you've come to the end of this tutorial. Please note that Bogus is capable of doing way more cool stuff.

You can find the code to this tutorial here on github.

You could define your own enums where it could randomly pick values of, It supports lots of locales. It knows a lot of datasets and definitions that you can instantly use and much much more.


development .net core tutorial